Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# web.config中的connectionstring错误:System.InvalidOperationException:实例失败_C#_Asp.net_Web Config - Fatal编程技术网

C# web.config中的connectionstring错误:System.InvalidOperationException:实例失败

C# web.config中的connectionstring错误:System.InvalidOperationException:实例失败,c#,asp.net,web-config,C#,Asp.net,Web Config,我正在使用Visual Studio社区版和C#和SQL Server Express 2014为数据库开发一个应用程序 当我在Web.config文件中定义连接字符串时,如下所示: <connectionStrings> <add name="connect" connectionString="data source=PTW\SQLEXPRESS; Database=Mydb; integrated security=SSPI"

我正在使用Visual Studio社区版和C#和SQL Server Express 2014为数据库开发一个应用程序

当我在
Web.config
文件中定义连接字符串时,如下所示:

<connectionStrings>
    <add name="connect" 
         connectionString="data source=PTW\SQLEXPRESS; Database=Mydb; integrated security=SSPI" 
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
我得到一个错误:

System.InvalidOperationException:实例失败

但是,当我在表单中定义连接字符串时,不会出现错误,并且会建立连接

我如何解决这个问题

以下是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>

问题来自SQL Server端,刚从microsoft(hotfix v4.microsoft.com)安装了SQL Server update(4),就这样,完成了。

什么是“ASP.net社区版”?另外,您的异常是来自问题中显示的代码行还是其他行?它是visual studio社区版2015。当连接尝试使用以下代码打开时,会出现错误:con.open();然后您需要提供该代码,因为它甚至可能与您的连接字符串无关。请创建一个。我添加了完整的代码
<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>
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";
            }
        }
    }
}