我用 C# 编写了一个代码,它在我的计算机上运行良好,在 Windows 7(MS SQL Server 2008)上运行,但在 Windows Vista(MS SQL Server 2005)上运行良好。我无法更改第二台计算机上的系统;)我使用的是 Visual Studio 2010。
所以这是代码的一部分,来自我的类“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)上运行相同的代码时,会发生错误(部分是其他语言,所以我为你翻译了它):
错误 SqlException: System.Data.SqlClient.SqlException (0x80131904) : 错误网络发生错误或连接到 SQL Server 时发生错误。找不到服务器或不可用。验证实例名称是否正确以及 SQL Server 的配置是否允许远程连接。(提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接) 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔 breakConnection) 在 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)在 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 ) 在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) 在 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) 在 System.Data.ProviderBase .DbConnectionPool。CreateObject(DbConnection owningObject) 在 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) 在 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) 在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) 在系统。 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(对象发送者,EventArgs e)GetConnection(DbConnection owningObject) 在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) 在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) 在 System.Data.SqlClient.SqlConnection.Open() 在 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) 在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) 在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) 在 System.Data.SqlClient.SqlConnection.Open() 在 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)
当我运行使用相同数据库的其他程序时,它连接正确,所以我认为登录等是正确的......我希望:) 但是第二个程序是商业的,所以我没有它的源代码,我只给他我的dbdir,实例,用户名和密码。
那么……我该怎么办?