问题
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.
So this is the part of code, from my 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();
}
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):
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)
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.
So... what can I do?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/18056493/sqlexception-system-data-sqlclient-sqlexception-0x80131904