通过开关将您的应用程序连接到数据库 [英] connect your app to database through switch
本文介绍了通过开关将您的应用程序连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在开发c#应用程序.我想通过交换机连接到另一台计算机上的数据库,该怎么办?
hello guys,
I''m developing c# app . I want to connect to database on different computer through switch ,how can I do it?
推荐答案
问题出在哪里?将连接字符串中的"localhost"替换为运行数据库的计算机的名称.
取决于数据库引擎(哪一个-您为什么不告诉我们?),您可能必须对其进行配置,以接受来自不同计算机的连接.应当将防火墙配置为允许通信.
Where is the problem? Replace "localhost" in your connection string with the name of the computer the database is running on.
Depending on the database engine (which one - why don''t you tell us?) you may have to configure it to accept connections from different machines. The firewalls should be configured to allow the communication.
在web \ app配置的connectionStrings部分中,指定不同的连接字符串:-
In the connectionStrings section in your web\app config you specify the different connection strings:-
<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.
问题出在哪里?将连接字符串中的"localhost"替换为运行数据库的计算机的名称.
取决于数据库引擎(哪一个-您为什么不告诉我们?),您可能必须对其进行配置,以接受来自不同计算机的连接.防火墙应配置为允许通信.
Where is the problem? Replace "localhost" in your connection string with the name of the computer the database is running on.
Depending on the database engine (which one - why don''t you tell us?) you may have to configure it to accept connections from different machines. The firewalls should be configured to allow the communication.
这篇关于通过开关将您的应用程序连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文