[英].NET Core System.InvalidOperationException: Instance failure
[英]connectionstring in web.config error : System.InvalidOperationException: Instance failure
我正在使用将Visual Studio Community Edition与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>
并使用以下语句检索它:
string cs = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
我收到一个错误:
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=\"Web\" /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";
}
}
}
}
问题出在SQL Server方面,刚刚从Microsoft(hotfixv4.microsoft.com)安装了SQL Server更新(4),仅此而已,完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.