所以这是怎么回事。我刚刚在 Visual C# 中创建并“发布”了一个员工管理工具。在开发过程中,我使用保存在 Properties.Settings.Default 中的字符串连接到我用于开发的数据库。现在解决方案已经发布并准备就绪,老板想要连接到真实的员工数据库。我的印象是连接到新数据库就像在某处更改某些属性文件中的连接字符串一样简单。不幸的是,我似乎找不到合适的文件/字符串来连接到我想要的数据库。有什么想法吗?
谢谢! JB
最佳答案
看这里:
Connection Strings and Configuration Files
通过使用配置文件,您只需在部署应用程序后更改配置文件连接字符串。
这是一种做你想做的事情的方法:
来自 http://www.dreamincode.net/forums/topic/70745-connection-string-in-appconfig/
你的配置文件内容:
<connectionStrings >
<add name="YourName"
connectionString="Provider=msdaora;Data Source=MyOracleDB;Persist Security Info=False;Integrated Security=Yes;"
providerName="System.Data.OracleClient" />
</connectionStrings>
运行时获取连接字符串的方法:
public static string GetConnectionString(string strConnection)
{
//Declare a string to hold the connection string
string sReturn = new string("");
//Check to see if they provided a connection string name
if (!string.IsNullOrEmpty(strConnection))
{
//Retrieve the connection string fromt he app.config
sReturn = ConfigurationManager.ConnectionStrings(strConnection).ConnectionString;
}
else
{
//Since they didnt provide the name of the connection string
//just grab the default on from app.config
sReturn = ConfigurationManager.ConnectionStrings("YourConnectionString").ConnectionString;
}
//Return the connection string to the calling method
return sReturn;
}
使用方法:
string connectionString = GetConnectionString("YourName");
关于c# - 如何将已发布的 Visual C# 解决方案连接到不同的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3170020/