gpt4 book ai didi

c# - SqlException:System.Data.SqlClient.SqlException (0x80131904)

转载 作者:行者123 更新时间:2023-11-30 20:55:49 25 4
gpt4 key购买 nike

我用 C# 编写了一段代码,它在我的计算机上运行良好,在 Windows 7 (MS SQL Server 2008) 上运行,但在另一台计算机上运行 Windows Vista (MS SQL Server 2005) 时效果不佳。我无法在第二台计算机上更改系统 ;) 我正在使用 Visual Studio 2010。

所以这是代码的一部分,来 self 的类(class)“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();
}

它在我的电脑上运行良好(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)

当我运行其他使用相同数据库的程序时,它会正确连接,所以我认为登录等是正确的...我希望 :) 但是第二个程序是商业程序,所以我没有它的源代码,我只给他我的 dbdir、实例、用户名和密码。

那么……我能做什么?

最佳答案

此错误不是登录问题。这不是无效凭据的问题。你会得到一个不同的错误信息。此错误消息仅表示它无法在连接字符串中找到服务器(可能)或数据库。

CodePlex 上有一个开源程序,您可以下载它来测试连接字符串等。You can find it here .这应该可以帮助您确定问题出在哪里/出了什么问题。

关于c# - SqlException:System.Data.SqlClient.SqlException (0x80131904),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18056493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com