0

尝试从 c# 代码连接到 ms sql,但收到此错误。如果我像这样进行简单的连接:

  String connect = "server=MY-PC\\SQLEXPRESS; database=mydb; Integrated Security=SSPI;";
          con.ConnectionString = connect;

但是当我从 app.config 尝试 connecto 时,我收到了这个错误:

      String connect = ConfigurationManager.AppSettings["connectionString"];
         con.ConnectionString = connect ;

这是xml代码:

    <configuration>
      <appSettings>
       <add key="connectionString" value="server=MY-PC\\SQLEXPRESS; database=mydb;       Integrated        Security=SSPI;"/>
      </appSettings>
     </configuration>

一些想法?

4

2 回答 2

0

尝试这个

String connect = @"数据源=.\SQLEXPRESS;AttachDbFilename=mydb;Integrated Security=True;" con.ConnectionString = 连接;

于 2013-04-17T06:02:16.703 回答
0

您应该像这样在 app.config 中设置连接字符串

<connectionStrings>
<add name="connect" connectionString="Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated 
Security=True;User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>

然后让你的连接字符串为

ConfigurationManager.ConnectionStrings["connect"].ConnectionString

示例 Conn 字符串:

<add name="NorthWind" 
       providerName="System.Data.SqlClient" 
       connectionString="server=.;database=NorthWind;Integrated Security=SSPI" />
于 2012-05-09T16:49:22.000 回答