根据数据库路径更改连接字符串 [英] change connection string according to path of databse
本文介绍了根据数据库路径更改连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我想根据数据库的路径将连接字符串更改为动态,而不是自己写:
hi all
I want to change connection string according to path of database to be dynamic not to write it by my self:
SqlConnection conn = new SqlConnection("Data Source=MS-TOSHIBA;Initial Catalog=dalily system;Integrated Security=True");
推荐答案
您好可以使用以下方法
Hi you can use the following approach
String ConnString = string.Empty;
switch (DB)
{
case "ABC":
ConnString = "Data Source=MS-TOSHIBA;Initial Catalog=dalily system;Integrated Security=True";
break;
case "DEF":
ConnString = "Data Source=MS-TOSHIBA2;Initial Catalog=dalily system;Integrated Security=True";
break;
case "XYZ":
ConnString = "Data Source= "+DB+";Initial Catalog=dalily system;Integrated Security=True";
break;
default:
ConnString = "Data Source=MS-MYHOME;Initial Catalog=dalily system;Integrated Security=True";
break;
}
SqlConnection conn = new SqlConnection(ConnString);
您是否可以像案例1和案例2一样直接使用硬编码连接字符串? 2,或者您可以在运行时创建它(如XYZ
所述)
希望对您有所帮助....cheerz
Can you directly use hardcode connection string as it is done in Case 1 & 2 or you can create it at run time as mentioned in case XYZ
Hope it helps.... cheerz
http://msdn .microsoft.com/en-us/library/ms178411.aspx [ ^ ]
这篇关于根据数据库路径更改连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文