1

我收到“未处理 InvalidOperationException”。con.Open(); 然后突出显示。

如果我在 .cs 文件中使用连接字符串,我的应用程序工作正常,但如果我将其放在 web.config 中,则会出现异常。尝试了谷歌上所有可用的东西,但没有运气。

代码

using System.Configuration;
using System.Web.Configuration;

public DataSet GetInvoice()
{
      SqlConnection con = new SqlConnection();
     con.ConnectionString =            WebConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; 

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("select * from invoice", con);
            **con.Open();**
            da.Fill(ds);
            con.Close();
            return ds;
        }

网页配置

<connectionStrings>       
    <add name ="MyConnection" connectionString = "Data Source =.\\SQLEXPRESS; AttachDbFilename = C:\\USERS\\SALIL\\DOCUMENTS\\INVOICING.MDF ; Integrated Security = True" providerName="System.Data.SqlClient" />
  </connectionStrings> 

堆栈跟踪:

 at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at WebInvoicing.Invoice.GetInvoice() in C:\WebInvoicing\WebInvoicing\Invoice.cs:line 220
   at WebInvoicing.InvoiceApp.LoadGrid() in C:\WebInvoicing\WebInvoicing\InvoiceApp.aspx.cs:line 124
   at WebInvoicing.InvoiceApp.Page_Load(Object sender, EventArgs e) in C:\WebInvoicing\WebInvoicing\InvoiceApp.aspx.cs:line 13
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
4

1 回答 1

3

无需转义 web.config 文件中连接字符串中的斜杠。它应该是:

  <connectionStrings>       
    <add name ="MyConnection" 
       connectionString = "Data Source =.\SQLEXPRESS; AttachDbFilename = C:\USERS\SALIL\DOCUMENTS\INVOICING.MDF ; Integrated Security = True" 
       providerName="System.Data.SqlClient" />
  </connectionStrings> 

有关详细信息,请参阅处理连接字符串中的反斜杠

于 2012-09-23T22:30:24.803 回答