何我在C#中为Sql Server使用多个连接字符串 [英] Ho Do I Use Multiple Connection String In C# For Sql Server
本文介绍了何我在C#中为Sql Server使用多个连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将数据从一台服务器插入另一台服务器。我正在使用两个连接字符串。但是在insert命令中我应该如何定义连接字符串,我的意思是我如何定义哪个数据库属于哪个服务器
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
推荐答案
没有测试它但它应该工作 -
在web.config中定义两个连接字符串
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
var conStringDQL= ConfigurationManager.ConnectionStrings["conStringDQL"].ConnectionString;
var conStringDML= ConfigurationManager.ConnectionStrings["conStringDML"].ConnectionString;
现在,使用 conStringDQL 来检索数据,使用 conStringDML 来插入/更新数据。
希望,它有帮助:)
Now, use conStringDQL to retrive data and conStringDML to insert/update data.
Hope, it helps :)
这篇关于何我在C#中为Sql Server使用多个连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文