場景:IIS默認站點建立程序,使用Windows集成身份驗證方式,連接SQLServer數據庫也是采用集成身份驗證。我報“System.Data.EntityException: The underlying provider failed on Open.”這個錯誤是因為計算機登錄密碼改過,導致之前應用程序池設置的憑證錯誤,解決辦法:把應用程序池的登錄憑證也修改一下改成和計算機登錄密碼一樣,這樣程序就好了。
步驟:
新增一個應用程序:
IIS Default Web Site-->Add Application-->Application Pool-->ASP.NET V4.0 Integrated
修改應用程序池的憑證:
Application Pool-->ASP.NET V4.0 Integrated-->Advanced Settings-->Process Model-->Identity-->Customer account-->Set Credentials-->Set UserName=Cumputer LoginName,Password=Cumputer Login Password-->Click OK
配置文件

<appSettings> <add key="CreditCheckGatewayDB" value="Server=fscnbesd0001;Database=CreditCheckGateway;Integrated Security=SSPI;" /> </appSettings>
C#讀取連接字符串

static public string GetCupCreditCheckConnectionString { get { return string.Format("metadata=res://*/CupModel.csdl|res://*/CupModel.ssdl|res://*/CupModel.msl;provider=System.Data.SqlClient;provider connection string=\"{0}\";", ConfigurationManager.AppSettings["CreditCheckGatewayDB"]); } }
或者配置文件直接寫

<appSettings> <add key="CupCreditCheckDB" value="metadata=res://*/CupModel.csdl|res://*/CupModel.ssdl|res://*/CupModel.msl;provider=System.Data.SqlClient;provider connection string="Server=fscnbesd0001;Database=CreditCheckGateway;Integrated Security=SSPI"" providerName="System.Data.EntityClient" /> </appSettings>

<connectionStrings> <add name="CupCreditCheckDB" connectionString="metadata=res://*/CupModel.csdl|res://*/CupModel.ssdl|res://*/CupModel.msl;provider=System.Data.SqlClient;provider connection string="Server=fscnbesd0001;Database=CreditCheckGateway;Integrated Security=SSPI"" providerName="System.Data.EntityClient" /> </connectionStrings>