无法从app config读取连接字符串

时间:2015-10-08 04:30:50

标签: 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];
  

例外:无法连接到服务器数据源= PC210090 \ SQLEXPRESS;初始目录=主;持久安全信息=真;                用户ID = sa;密码=密码-1; multipleactiveresultsets = True;

1 个答案:

答案 0 :(得分:0)

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

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 connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CostAllocationEntities2"].ConnectionString);

在您的代码中

一旦你有连接字符串创建一个SqlConnection实例,如

connection.Open();

打开连接

context.currentTime