c# - 无法从应用程序配置读取连接字符串

标签 c# mysql connection server

这是我在 App.config 中的连接字符串

<connectionStrings>
<add name="CostAllocationEntities2" connectionString="Data Source=PC210090\SQLEXPRESS; Initial Catalog=master;Persist Security Info=True;
         User ID=sa;password=password-1;multipleactiveresultsets=True; " providerName="System.Data.SqlClient"/>

</connectionStrings>

     ServerConnection connection = new ServerConnection(ConfigurationManager.ConnectionStrings["CostAllocationEntities2"].ConnectionString);

                    Server sqlServer = new Server(connection);

                    Database db = sqlServer.Databases[strDBName];

Exception : Failed to connect to server Data Source=PC210090\SQLEXPRESS; Initial Catalog=master;Persist Security Info=True; User ID=sa;password=password-1;multipleactiveresultsets=True; .

最佳答案

您想做什么,不确定,但更改您的连接字符串

connectionString="数据源=PC210090\\SQLEXPRESS;

初始目录=master;;不要连接到 master 数据库,而是提供一个用户数据库名称,例如 Sample

<connectionStrings>
<add name="CostAllocationEntities2" connectionString="Data Source=PC210090\\SQLEXPRESS; Initial Catalog=Sample;Persist Security Info=True; User ID=test;password=test;multipleactiveresultsets=True; " providerName="System.Data.SqlClient"/>
</connectionStrings>

在你的代码后面

获得连接字符串后,创建一个 SqlConnection 实例,例如

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CostAllocationEntities2"].ConnectionString);

打开连接

connection.Open();

关于c# - 无法从应用程序配置读取连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33006671/

相关文章:

php - 无法在PHP中与fsockopen连接(连接超时)

c# - 对数据库更改执行 .NET 操作

c# - 如何迭代多维数组的行和列?

Mysql join 给出重复行

mysql - 如何使用 ALTER 命令将 int 数据类型的列添加到 SQL 中的表中?

php - 更改表修改列查询结果出现 SQL 语法错误 1064

c# - 如何保存MySql连接?

c# - IIS 反向代理干扰重定向位置 header

c# - ServiceStack Jsv 序列化程序无法反序列化 Dictionary<DateTime, ___>

php - 当我检查代码时不断出现语法错误