2016-10-05 140 views
0

我在使用SQL Server设置基于C#的网站的开发实例时遇到了一些麻烦。我习惯使用MySQL来完成这一切,所以这对我来说有点陌生。阅读了MS故障排除之后,我的设置看起来没问题,但这仍然不起作用,所以很明显我在某个地方出现故障。无法将网站连接到SQL Server

我已经使用以下配置将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): The system cannot find the file specified]

[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to 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. (provider: Named Pipes Provider, error: 40 - Could not open a connection to 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 
+0

服务器引擎是否实际在服务器上运行?问题可能出在服务器本身上。你可以在服务器上运行'SQL Management Studio'并使用连接字符串中的凭证连接到数据库吗? – Sparrow

+0

在此网站上搜索* sql-server建立连接时发生网络相关或实例特定的错误*。实际上有几十个(如果不是更多的话)现有问题(和答案)可以帮助解决这个问题。 –

+0

是的,我可以使用SQL Management Studio连接到服务器。当我设置时,主机不得不添加我的本地IP以允许远程访问SQL服务器,也许Web服务器IP不允许访问?尽管他们确实设置了整个环境,所以我希望如此 - 仍然在等着听那张票。 – user1235285

回答

0

错误本身就已经表示,web.config不正确的服务器配置:

A network-related or instance-specific error occurred while establishing a connection to 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. (provider: Named Pipes Provider, error: 40 - Could not open a connection to 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> 

关于HTTP 500错误的web.config加入Network Library=DBMSSOCN到连接字符串时,请参阅下面的更多细节取决于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

0

谢谢大家的输入,原来的托管公司(尽管声称一切都很好),分别为有过错。非常抱歉浪费了任何人的时间,让他们承认错误已经有点令人沮丧了!最后,我们设置了一个单独的MS SQL服务器并且连接得很好。