c# - 如何将已发布的 Visual C# 解决方案连接到不同的数据库

标签 c# database connection-string

所以这是怎么回事。我刚刚在 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/

相关文章:

c# - 如何在 EF Core 表达式中使用继承的属性?

从 C++ DLL 调用时,带有参数的 C# 委托(delegate)回调会导致 AccessViolation

javascript - 使用 iOS 和 Javascript 解析数据库

oracle - 如何在本地计算机上复制 Oracle 11g 数据库(数据+结构)以进行开发?

.net - 从 Spring.NET xml 配置访问 ConfigurationManager.AppSettings 值

c# - 发布 C# 控制台应用程序后更改数据库连接字符串

c# - 如何通过单击 C# 中的按钮从另一个表单中隐藏一个表单?

c# - 如何在 c# 中的 xamarin android 中识别 android 移动设备 uniqueid

java - 搜索包含所有特殊字符的数据库表

excel - OLEDB中IMEX的默认值是多少?