Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
pls help.....
i want to use two different database in my one asp.net project. suppose i have 2 database A & B, i want to call some stored procedures from database A and some stored procedures call from database B. Then,
how to give 2 database connections in connectionstring?

Thanks in advance.....
Posted

In web.config, you can have two distinct (by name) connectionstring as:

HTML
<configuration>
  <connectionstrings>
    <add name="MyConnection1" connectionstring="Data Source=server1;Initial Catalog=db1;User ID=id1;Password=pass1;" providername="System.Data.EntityClient" />
    <add name="MyConnection2" connectionstring="Data Source=server2;Initial Catalog=db2;User ID=id2;Password=pass2;" providername="System.Data.EntityClient" />
  </connectionstrings>
  <system.web></system.web></configuration>


In the code behind, you can fetch these two connection in two different holder as:

C#
private static string connectionString1 = ConfigurationManager.ConnectionStrings["MyConnection1"].ToString();
private static string connectionString2 = ConfigurationManager.ConnectionStrings["MyConnection2"].ToString();


Hope, it clarify your question.
 
Share this answer
 
Comments
Varun Sareen 21-Feb-12 23:33pm    
my 5: good code snippet
Just use 2 connection string inside <connectionstrings></connectionstrings> block.

Use the one you need to connect the db when you call stored procedure.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900