Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 11-Jan-13 21:28pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
Comments
Shanu2rick at 12-Jan-13 4:17am
   
@Mika: Didn't knew about the Linked Servers. Thanks for sharing.
Mika Wendelius at 12-Jan-13 4:19am
   
You're welcome :)
CHill60 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Add Two ConnectionString in your Web.config file with different name's like this:
 
<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>
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 12 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100