Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends,

am creating a asp.net Web application. In this i want to use 2 Sqlserver databases.

I have two databases in Sqlserver2005 as StudentDB and CollegeDB. NOW i want to use these 2 databases in my program.

so how should i use, and how to store these two databases in web.configfile.

Please help.

I want to use 2 databases with one Asp.Net Application.

Please Help THANKS.
Posted

As already suggested you can store two different connection strings and then you can open two separate SqlConnection objects, on per each database.

However, to give you an alternative, you can also define a linked server in either of the Sql Server instances. Using linked server you can achieve a situation where you connect only to one database server which then again connects to another. This way you would need to have only one connection in your application.

To query the data 'behind' the linked server you need to use special four-part notation for tables. If this feels cumbersome you can always define a view to hide the location of a table.

More about linked servers: Linked Servers[^]
 
Share this answer
 
Comments
Shanu2rick 12-Jan-13 4:17am    
@Mika: Didn't knew about the Linked Servers. Thanks for sharing.
Wendelius 12-Jan-13 4:19am    
You're welcome :)
CHill60 26-Mar-13 6:09am    
Followed a link from another solution today (http://www.codeproject.com/Answers/567607/HowplustoplususeplusmultipleplusDBplusinplussingle#answer1) and found this solution ...so my +5
Add Two ConnectionString in your Web.config file with different name's like this:

C#
<connectionstrings>
    <add name="ConnectionString1" connectionstring="Initial Catalog=dbName1;Data Source=ServerName;Integrated Security=SSPI;" providername="System.Data.SqlClient" />
    <add name="ConnectionString2" connectionstring="Data Source=dbName2;Initial Catalog=ServerName;Integrated Security=True">
      providerName="System.Data.SqlClient" />
  </add></connectionstrings>
 
Share this answer
 

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