.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to use Multiple DataBase Connection String in Web.Config?

Posted By:Syed Shakeer Hussain       Posted Date: August 20, 2009    Points: 10    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

In this article you can learn how to store and use Multiple DataBase Connection sting in web.conf file?

How to create a web.config file?

You can check in below link how to creat a web.config in asp.net,

http://www.dotnetspark.com/kb/642-how-to-store-global-connection-string.aspx

Here i am using Two DataBases.

1)SqlServer database

2)MsAccess database.

Suppose i want to display two table informatiom on asp page.First table is located in SqlServer database and another one is located in MsAccess Database.

How to store a connection string in web.config file?

1)Storing Sqlserver DataBase connection string:

For SqlServer database we have to use Proivder name as

"System.Data.sqlClient"

<add name="textingconnection.Properties.Settings.shakeerConnectionString" connectionString="Data Source=INTHIYAAZ;Initial Catalog=shakeer;Persist Security Info=True;User ID=sa;Password=sa" providerName="System.Data.SqlClient" />

2)Storing MsAccess DataBase connection string:

For MsAccess database we have to wrte Proivder name as "SystemData.OleDb"

<add name="MS-db1ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\Documents and Settings\INTHIYAAZPS\My Documents\MSAccess\db1.mdb"" providerName="System.Data.OleDb" />

write the above connection string as below in web.config file

xml version="1.0"?>

<configuration>

<configSections>

configSections>

<connectionStrings>

<add name="textingconnection.Properties.Settings.shakeerConnectionString"

connectionString="Data Source=INTHIYAAZ;Initial Catalog=shakeer;Persist Security Info=True;User ID=sa;Password=sa"

providerName="System.Data.SqlClient" />

<add name="MS-db1ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\Documents and Settings\INTHIYAAZPS\My Documents\MSAccess\db1.mdb""

providerName="System.Data.OleDb" />

connectionStrings>

<system.web>

<compilation debug="true"/>

system.web>

configuration>

In .aspx.cas page add namespace as :

1)for SqlServer-Using System.Data.SqlClient;

SqlConnection conn ;

conn= new SqlConnection(ConfigurationManager.ConnectionString["textingconnection.Properties.Settings.shakeerConnectionString"].ConnectionString);

conn.Open();

2)for MsAccess-Using System.Dat.OledClient;

OleDbConnection conn_MSACCESS;

 conn_MSACCESS= new OleDbConnection(ConfigurationManager.ConnectionStrings["MS-db1ConnectionString"].ConnectionString);

conn_MSACCESS.Open();

wrie the Connection string in Pageload as follows:

protected void Page_Load(object sender, EventArgs e)

{

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["textingconnection.Properties.Settings.shakeerConnectionString"].ConnectionString);

conn.Open();

Response.Write("connection Connected sucessfully"+
);

conn.Close();

OleDbConnection conn_MSACCESS = new OleDbConnection(ConfigurationManager.ConnectionStrings["MS-db1ConnectionString"].ConnectionString);

conn_MSACCESS.Open();

Response.Write("MS_ACCESS_connection Connected sucessfully");

conn_MSACCESS.Close();

}

Thanks for Reading my artilce

Syed Shakeer Hussain


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend