繁体   English   中英

无法将网站连接到SQL Server

[英]Unable to connect website to 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

该错误本身已经表明web.config上的服务器配置不正确:

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

由于显示“找不到服务器”,因此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错误,有关HResult代码的更多详细信息,请参见下面的MS KB参考。 如果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

C#的远程SQL Server的正确连接字符串

远程SQL Server的ConnectionString

谢谢大家的投入,事实证明托管公司(尽管声称一切都很好)都错了。 非常抱歉浪费任何人的时间,让他们也承认自己的过错已经让我感到沮丧! 最后,我们设置了一个单独的MS SQL服务器并连接正常。

暂无
暂无

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

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