本文介绍了通过开关将您的应用程序连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在开发c#应用程序.我想通过交换机连接到另一台计算机上的数据库,该怎么办?
hello guys,
I''m developing c# app . I want to connect to database on different computer through switch ,how can I do it?
推荐答案
<connectionstrings>
<add name="ConnString1" connectionstring="Data Source=serverinstancename;Initial Catalog=DBName;User Id=username;Password=password;" />
<add name="ConnString2" connectionstring="Data Source=serverinstancename;Initial Catalog=DBName;User Id=username;Password=password;" />
</connectionstrings>
那么在您的代码中,您可以有一个方法,该方法根据您的开关中的条件返回一个SQLConnection.
then in your code you could have a method which returns an SQLConnection based on the criteria in your switch
public SqlConnection getConnection()
{
string conn = "";
switch(yourDependancy)
{
case a:
conn = configurationManager.ConnectionStrings["ConnString1"].ConnectionString;
break;
case b:
conn = configurationManager.ConnectionStrings["ConnString2"].ConnectionString;
break;
}
return new SqlConnection(conn);
}
您显然将需要检查值是否存在\ nulls等,并且可能包括开关的默认情况,但这是做我想做的一种方法.
you will obviously need to check that values exist\nulls etc and maybe include a default case for your switch but this is one way of doing what I think you are after.
这篇关于通过开关将您的应用程序连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!