我有一个C#应用程序,它使用本地.mdf数据库文件存储一些数据。
我首先在安装了Visual Studio的另一台机器(Windows 10,64bit)上测试它,但是应用程序无法连接到localDB实例。安装Microsoft SQL Server 2014 Express(包含LocalDB安装包)后,它开始工作。
我决定在另一台机器上测试它(Windows 7,没有Visual Studio,32位),发生了同样的问题。安装Microsoft SQL Server 2014 Express无法解决问题。返回的错误消息指出:“SQL网络接口,错误:26 - 找到指定的服务器/实例时出错”。
我的应用程序中使用的连接字符串是:
string path = Directory.GetCurrentDirectory();
string path_to_local_DB = Directory.GetCurrentDirectory() + "\\" + "Monitoring.mdf";
string local_connection_string = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + path_to_local_DB + ";Integrated Security=True";
我还在使用Windows7的机器上检查了以下内容:
完整的堆栈跟踪:
---------------------------
Failed to connect to localDB. Stacktrace: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, 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 Zensys.ZWave.Programmer.UI.MonitoringForm.checkForUnsyncedEntries()
有人知道是什么原因导致应用程序在Windows 10计算机上运行而无法在Windows 7计算机上运行吗?两台机器都安装了.NET 4.6.2版。
答案 0 :(得分:0)
首先转到SQL服务器并执行以下命令:
SELECT @@ SERVERNAME
现在,使用Above结果更改 local_connection_string [数据源](在上面的代码中提及)。
它的所有连接问题。