C# SqlException: System.Data.SqlClient.SqlException (0x80131904)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18056493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SqlException: System.Data.SqlClient.SqlException (0x80131904)
提问by user2652682
I wrote a code in C#, which works great at my computer, with Windows 7 (MS SQL Server 2008) but not at the other with Windows Vista (MS SQL Server 2005). I can not change system on the second computer ;) I'm using Visual Studio 2010.
我用 C# 编写了一个代码,它在我的计算机上运行良好,在 Windows 7 (MS SQL Server 2008) 上运行良好,但在 Windows Vista (MS SQL Server 2005) 上运行时却没有。我无法在第二台计算机上更改系统;) 我使用的是 Visual Studio 2010。
So this is the part of code, from my class "obSQL":
所以这是代码的一部分,来自我的课程“obSQL”:
private SqlConnection connection;
public obSQL(string user, string pass, string instance, string dbdir) //sql server authentication
{
connection = new SqlConnection();
connection.ConnectionString = "user id=" + user + ";" +
"password=" + pass +
";Data Source=" + instance + ";" +
"Trusted_Connection=no;" +
"database=" + dbdir + "; " +
"connection timeout=3"; //more at http://www.connectionstrings.com/
connection.Open();
}
public obSQL(string instance, string dbdir) //windows authentication
{
connection = new SqlConnection();
connection.ConnectionString = "Data Source=" + instance + ";" +
"Trusted_Connection=yes;" +
"database=" + dbdir + "; " +
"connection timeout=3";
connection.Open();
}
It works great at my computer (SQL Server 2008). But when I run the same code at the other (SQL Server 2005) then occure an error (part of it is other language, so I translated it for you):
它在我的计算机(SQL Server 2008)上运行良好。但是当我在另一个(SQL Server 2005)上运行相同的代码时,出现了一个错误(部分是其他语言,所以我为你翻译了它):
Error SqlException: System.Data.SqlClient.SqlException (0x80131904): Error An error has occurred with the network or the occurrence when connecting to SQL Server. Can not find server or is not available. Verify that the instance name is correct and that the configuration of SQL Server allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at cennik01.obSQL..ctor(String user, String pass, String instance, String dbdir) at Cennik_v2._1.Form1.button1_Click(Object sender, EventArgs e)
错误 SqlException: System.Data.SqlClient.SqlException (0x80131904): 错误 网络或连接 SQL Server 时发生错误。找不到服务器或不可用。验证实例名称是否正确以及 SQL Server 的配置是否允许远程连接。(提供程序:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接) 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,Boolean breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() 在 System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) 处的 Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean IntegratedSecurity) serverInfo、String newPassword、Boolean redirectedUserInstance、SqlConnection owningObject、SqlConnectionString connectionOptions、TimeoutTimer 超时)在 System.Data。SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, Boolean redirectedUserInstance) ) 在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) at System.Data.ProviderBase .DbConnectionPool。CreateObject(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System. Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection externalConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at cennik01.obSQL..ctor(String user, String pass, String instance, String dbdir) at Cennik_v2._1 .Form1.button1_Click(对象发送者,EventArgs e)GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection externalConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at cennik01 .obSQL..ctor(String user, String pass, String instance, String dbdir) at Cennik_v2._1.Form1.button1_Click(Object sender, EventArgs e)GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection externalConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at cennik01 .obSQL..ctor(String user, String pass, String instance, String dbdir) at Cennik_v2._1.Form1.button1_Click(Object sender, EventArgs e)_1.Form1.button1_Click(对象发送者,EventArgs e)_1.Form1.button1_Click(对象发送者,EventArgs e)
When I run other program which is using the same database it connect correctly, so I think that login etc are correct... I hope :) But this second program is commercial, so I don't have it's source code, I only give him my dbdir, instance, user name and password.
当我运行其他使用相同数据库的程序时,它连接正确,所以我认为登录等是正确的......我希望:) 但是第二个程序是商业的,所以我没有它的源代码,我只给出他我的dbdir,实例,用户名和密码。
So... what can I do?
所以……我能做什么?
回答by Randy Minder
This error is not a login issue. It's not a matter of invalid credentials. You would get a different error message for that. This error message simply means it cannot find either the server (probably) or the database in the connection string.
此错误不是登录问题。这不是无效凭据的问题。您会收到不同的错误消息。此错误消息仅表示它在连接字符串中找不到服务器(可能)或数据库。
There is a open source program on CodePlex you can download to testing connection string etc. You can find it here. This should help you determine where/what the problem is.
CodePlex 上有一个开源程序,您可以下载以测试连接字符串等。 您可以在此处找到它。这应该可以帮助您确定问题出在哪里/是什么。
回答by Anil
To me it looks like in connection string you are calling your server by the machine name and SQL server is not enabled with Named Pipes. The solution to the problem is: either enable the named pipes in SQL Server configuration or in connection string provide the IP Address of the SQL Server instead of name.
对我来说,在连接字符串中,您通过机器名称调用服务器,并且 SQL 服务器未启用命名管道。问题的解决方案是:要么在 SQL Server 配置中启用命名管道,要么在连接字符串中提供 SQL Server 的 IP 地址而不是名称。