Click here to Skip to main content
15,124,736 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to insert data from one server to another. I am using two connection strings for that. But in insert command how should i define that connection string, I mean how can I define which database belongs to which server
Posted
Updated 25-Apr-21 6:51am
v3
Comments
Andy Lanng 17-Apr-15 5:17am
   
If the databases are on different servers then you cannot.
Well, you can but you need to do a lot of setup on one of the servers so it can see the other and run commands on it. In 99.9% of cases it just isn't worth it.

there are other ways to get the same effect, though
Can you read from one connection and then write to another, or is the data too big?
John C Rayan 17-Apr-15 5:40am
   
What's the issue are you facing in having two different connections and using one with SELECT and another one with INSERT. Can you elaborate?

1 solution

Haven't test it but it should work-
Define two connectionstrings in web.config
<connectionstrings>
  <add name="ConStringDQL">
       providerName="System.Data.SqlClient"
       connectionString= "server=[server1];database=[db name];uid=[user name];pwd=[password]" ></add>
  <add name="ConStringDML">
       providerName="System.Data.SqlClient"
       connectionString= "server=[server2];database=[db name];uid=[user name];pwd=[password]" ></add>
</connectionstrings>

Then create two separate instance of connections using following connection strings
C#
var conStringDQL= ConfigurationManager.ConnectionStrings["conStringDQL"].ConnectionString;
 var conStringDML= ConfigurationManager.ConnectionStrings["conStringDML"].ConnectionString;

Now, use conStringDQL to retrive data and conStringDML to insert/update data.

Hope, it helps :)
   
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