Welcome

首页 / 软件开发 / C# / C#的winform中使用数据库连接字符串

C#的winform中使用数据库连接字符串2010-12-28在解决方案中增加一窗口选择xml文件,并将名称改为app.config.其内容设置如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="source" value="Server=localhost;uid=sa;pwd="fang";database=khgx;" />
</appSettings>
</configuration>

请在引用中添加-System.configuration不然程序会提示错误。在程序中就可以这样调用:

private string source = System.Configuration.ConfigurationManager.AppSettings["source"];
conn = new SqlConnection(source);
打包安装后,可以直在安装的机器上面接修改配置文件,进行数据库连接。