Click here to Skip to main content
15,886,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have four databases names Server1, Server2, Server3, and Server4 which contains a single table each.

SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["Server1"].ToString());
   SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Server2"].ToString());
   SqlConnection con3 = new SqlConnection(ConfigurationManager.ConnectionStrings["Server3"].ToString());
   SqlConnection con4 = new SqlConnection(ConfigurationManager.ConnectionStrings["Server4"].ToString());


i want to fire the following command in C# :-

SQL
select id, filename, Server from Server1.dbo.UploadedData where userid='1'
union
select id, filename,Server from Server3.dbo.UploadedData where userid='1'
union
select id, filename,Server from Server3.dbo.UploadedData where userid='1'
union
select id, filename,Server from Server4.dbo.UploadedData where userid='1'


for executing this command i have to pass connection object of each database.

like...
cmd= new SqlCommand("select id, filename, Server from Server1.dbo.UploadedData where userid='1'union select id, filename,Server from Server3.dbo.UploadedData where userid='1' union select id, filename,Server from Server3.dbo.UploadedData where userid='1' union select id, filename,Server from Server4.dbo.UploadedData where userid='1',con1,con2,con3,con4 ");


this is not possible to pass 4 connection object.......what will be the other best option??
Posted

It's impossible! The way you're trying to reach that is going to nowhere ;(

If you want to do it, please read about linked servers[^].
How to Create a Linked Server[^]

After that, you'll be able to get data from all databases in a single command ;)
 
Share this answer
 
v2
Comments
Pranav-BiTwiser 6-Mar-14 2:19am    
u mean to say linked server databses which has same server name?? or ....what?
Pranav-BiTwiser 6-Mar-14 2:21am    
You cannot create a local SQL Server as a linked server
Maciej Los 6-Mar-14 13:53pm    
Did you follow linked articles?
As per my knowledge it is not possible, because every database provides different connection string. Therefore you must explicitly need to specify each and every connection objects in command.
But still you can look at here[^]

-KR
 
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