![](/img/trans.png)
[英].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.