无法从应用配置中读取连接字符串

本文关键字:读取 连接 字符串 配置 应用 | 更新日期: 2023-09-27 18:16:45

这是我在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];

异常:连接服务器失败=PC210090'SQLEXPRESS;初始目录=master;持久安全信息=True;用户ID = sa;密码=密码1;multipleactiveresultsets = True;.

无法从应用配置中读取连接字符串

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

connectionString="Data Source=PC210090''SQLEXPRESS;

Initial Catalog=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();