简体   繁体   English

将C#Web应用程序连接到SQL Server时出错

[英]Error connecting C# web application to SQL Server

I'm working on a newly set up laptop. 我正在使用新设置的笔记本电脑。 There are two web projects I usually work with. 我通常使用两个Web项目。 One is fine. 一个很好。 The one giving me trouble is a C# Web forms application (ASP.NET 4.5, EF 6.0). 给我带来麻烦的一个是C#Web表单应用程序(ASP.NET 4.5,EF 6.0)。

The following is the error I get when trying to connect to the database via the application - 以下是尝试通过应用程序连接到数据库时遇到的错误-

Exception: 例外:

The underlying provider failed on Open. 基础提供程序在打开时失败。

Inner Exception: 内部异常:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.) (提供者:TCP提供程序,错误:0-无法建立连接,因为目标计算机主动拒绝了它。)

Debugging using IIS Express and ASP.NET Development Server give me the same result. 使用IIS Express和ASP.NET开发服务器进行调试的结果相同。 My connection string is also good - it's more or less the same as the one in the other project I use, bar their actual names. 我的连接字符串也很好-与我使用的另一个项目中的字符串基本相同,但不注明其实际名称。 For clarity here it is: 为了清楚起见,这里是:

<connectionStrings>
  <clear/>
  <add name="RetailerEntities" connectionString="Data Source=localhost;Network Library=DBMSSOCN;Initial Catalog=db;user id=sa;password=password;MultipleActiveResultSets=true;"/>
  <add name="RetailerCContext" connectionString="metadata=res://*/Providers.Model.csdl|res://*/Providers.Model.ssdl|res://*/Providers.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost;Network Library=DBMSSOCN;Initial Catalog=db;user id=sa;password=password;MultipleActiveResultSets=true;&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

I should also mention, both projects I work with access the same database, so it is strange that one would connect and one wouldn't. 我还要提到的是,我使用的两个项目都访问相同的数据库,因此很奇怪,一个会连接而一个不会。

Funnily enough, there are no issues with this project once it's deployed into a production environment, so it looks to just be a local issue on my laptop. 有趣的是,该项目一旦部署到生产环境中,就不会有任何问题,因此看起来只是笔记本电脑上的本地问题。 This makes me wonder is it some sort of permissions issue. 这让我怀疑是某种权限问题。

The stack trace is: 堆栈跟踪为:

   at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
   at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__9()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Retailer.Library.DB.ConfigurationController.GetConfigurations() in c:\Development\Retail\RetailerC.Library\DB\ConfigurationController.cs:line 41
   at RetailerC.Web.Default.BindDdlSearchUrl() in c:\Development\Retail\RetailerC.Web\Default.aspx.cs:line 449
   at RetailerC.Web.Default.Page_Load(Object sender, EventArgs e) in c:\Development\Retail\RetailerC.Web\Default.aspx.cs:line 79

If anyone had any ideas, or has experienced anything similar, I'd be grateful to hear. 如果有人有任何想法或经历过类似的事情,我将不胜感激。

I removed 'Network Library=DBMSSOCN;' 我删除了“网络库= DBMSSOCN;” from the connection strings and it seems to be working fine now. 从连接字符串,现在似乎工作正常。 Not sure why, but it's done it for me.. 不知道为什么,但是为我做了。

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

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