无法连接到LocalDB实例

时间:2016-12-08 11:51:51

标签: c# .net sql-server localdb

我有一个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的机器上检查了以下内容:

  • 我使用命令“sqllocaldb.exe create”(创建名为MSSQLLocalDB的实例)创建了一个LocalDB实例,并检查它是否已创建/正在运行“SqlLocalDb info”(返回所有正在运行的LocalDB实例)。
  • 检查SQL Server 2014的配置管理器中SQL服务器的状态是否已设置为“正在运行”
  • 在services.msc中检查服务“SQL Server”和“SQL Server Browser”都在运行。
  • 还尝试通过将“server name”字段设置为“(LocalDb)\ MSSQLLocalDB”来手动连接到SQL Server 2014 Management Studio中的LocalDB实例。它已连接。

完整的堆栈跟踪:

---------------------------
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版。

1 个答案:

答案 0 :(得分:0)

首先转到SQL服务器并执行以下命令:

SELECT @@ SERVERNAME

现在,使用Above结果更改 local_connection_string [数据源](在上面的代码中提及)。

它的所有连接问题。