Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
859 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...