无法将网站连接到SQL Server

时间:2016-10-05 01:47:40

标签: c# sql-server

我在使用SQL Server设置基于C#的网站的开发实例时遇到了一些麻烦。我习惯用MySQL做这一切,所以这对我来说有点陌生。通过MS故障排除阅读后,我的设置似乎没问题,但这仍然无法正常工作,所以很明显我在某处不知所措。

我已使用以下配置将web.config文件设置为指向新数据库;

<connectionStrings>
    <add name="External" 
         connectionString="Data Source=91.208.99.2,33114; Initial Catalog=sqldatabase;User ID=sqluser;Password=1234567890" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

但是,连接仍然失败,并出现以下错误。我有什么想法可以丢失吗?

  

[Win32Exception(0x80004005):系统找不到指定的文件]

     

[SqlException(0x80131904):建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供程序:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)]

这是堆栈跟踪的其余部分;

System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +6569310
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +717
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +6595752
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +219
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +6598063
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +6598643
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData) +942
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +1162
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +72
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +6601897
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +103
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +2102
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +116
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +1079
   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +6606391
   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +233
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +278
   System.Data.SqlClient.SqlConnection.Open() +239
   SubSonic.SqlDataProvider.CreateConnection(String newConnectionString) +48
   SubSonic.AutomaticConnectionScope..ctor(DataProvider provider) +58
   SubSonic.SqlDataProvider.GetDataSet(QueryCommand qry) +383
   SubSonic.StoredProcedure.GetDataSet() +64
   _usercontrols_layout_header.Page_Load(Object sender, EventArgs e) in \\sambad\sites\dev.aciemgroup.co.uk\public_html\_usercontrols\layout\header.ascx.cs:57
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178

2 个答案:

答案 0 :(得分:0)

错误本身已在web.config上说错误的服务器配置:

  

发生与网络相关或特定于实例的错误   建立与SQL Server的连接。找不到服务器或   无法访问。验证实例名称是否正确   SQL Server配置为允许远程连接。 (提供者:命名   管道提供程序,错误:40 - 无法打开与SQL Server的连接)

由于它说&#34;服务器未找到&#34;,SQL Server连接字符串可能成为主要问题。

如果您在远程服务器上有一个SQL Server实例/服务器名称(例如SERVERNAME),请在IP地址或机器名后附加它,就在服务器端口连接之前:

<connectionStrings>
<add name="External" connectionString="Data Source=91.208.99.2\SERVERNAME,33114; Initial Catalog=sqldatabase;User ID=sqluser;Password=1234567890" providerName="System.Data.SqlClient"/>
</connectionStrings>

对于SQL Server Express实例,连接字符串应如下所示:

<connectionStrings>
<add name="External" connectionString="Data Source=91.208.99.2\SQLEXPRESS,33114; Initial Catalog=sqldatabase;User ID=sqluser;Password=1234567890" providerName="System.Data.SqlClient"/>
</connectionStrings>

Network Library=DBMSSOCN添加到web.config上的连接字符串时,关于HTTP 500错误,有关详细信息,请参阅下面的MS KB参考,具体取决于HResult代码。如果HResult代码返回0x8007000d,则需要正确修改连接字符串内的目标服务器,如上所述。

添加网络库后的连接字符串应为:

<connectionStrings>
<add name="External" connectionString="Data Source=91.208.99.2\SERVERNAME,33114;Network Library=DBMSSOCN;Initial Catalog=sqldatabase;User ID=sqluser;Password=1234567890" providerName="System.Data.SqlClient"/>
</connectionStrings>

PS:检查并修改远程服务器的其他安全/机器/用户权限(如果有),确保之后数据库连接正常工作。

参考文献:

MS KB 942055 - https://support.microsoft.com/en-us/kb/942055

The right connection string for Remote SQL server for C#

ConnectionString for remote SQL Server

答案 1 :(得分:0)

谢谢大家的投入,结果托管公司(尽管声称一切都很好),都有错。非常抱歉浪费了任何时间,让他们承认过错也不仅有点令人沮丧!最后,我们设置了一个单独的MS SQL服务器,连接得很好。