本文介绍了如何在C Sharp项目中使用多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C Sharp项目中使用多个数据库。有人可以帮帮我吗?

How to use multiple database in a C Sharp Project. Can anyone help me, please?

推荐答案


<configuration>
  <connectionstrings>
    <add name="cs_local">
    connectionString="Data Source=(local);Initial Catalog=YourDB;Integrated Security=True"/>
    <add name="cs_remote">
    connectionString="Data Source=YourServer;Initial Catalog=YourDB;Integrated Security=True"/>
  </add></add></connectionstrings>
</configuration>



然后,在您的代码隐藏中,您可以这样做:


And then, in your code-behind, you can do that :

string cs_name = string.Empty;
if (your condition)
    cs_name = "cs_local";
else
    cs_name = "cs_remote";
var connectionString = ConfigurationManager.ConnectionStrings[cs_name].ConnectionString;



别忘了在Using Directives中添加System.Configuration;)


Don't forget to add System.Configuration in your Using Directives ;)



这篇关于如何在C Sharp项目中使用多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:32