简体   繁体   English

web.config中的connectionstring错误:System.InvalidOperationException:实例故障

[英]connectionstring in web.config error : System.InvalidOperationException: Instance failure

I am developing an app using Visual Studio Community Edition with C# and SQL Server Express 2014 for the database. 我正在使用将Visual Studio Community Edition与C#和SQL Server Express 2014一起用于数据库的应用程序。

When I define the connection string in Web.config file as shown below: 当我在Web.config文件中定义连接字符串时,如下所示:

<connectionStrings>
    <add name="connect" 
         connectionString="data source=PTW\SQLEXPRESS; Database=Mydb; integrated security=SSPI" 
         providerName="System.Data.SqlClient"/>
  </connectionStrings>

And retrieve it with the following statement: 并使用以下语句检索它:

string cs = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;

I get an error: 我收到一个错误:

System.InvalidOperationException: Instance failure. System.InvalidOperationException:实例故障。

But when I define the connection string inside a form, I get no error and a connection will be established. 但是,当我在表单中定义连接字符串时,不会出现任何错误,并且将建立连接。

How can I resolve this problem? 我该如何解决这个问题?

here's what i have in the web.config file: 这是我在web.config文件中所拥有的:

<configuration>
  <connectionStrings>
    <add name="connect" 
         connectionString="data source=PTW\SQLEXPRESS; Database=Mydb; integrated security=SSPI" 
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>

and this what i have inside the form : 这就是我在表单中的内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace GlobalCS
{
    public partial class Connect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string cs = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();
                Label1.Text = "Connected";
            }
        }
    }
}

问题出在SQL Server方面,刚刚从Microsoft(hotfixv4.microsoft.com)安装了SQL Server更新(4),仅此而已,完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM