简体   繁体   中英

Can't connect to the LocalDB instance

I have a C# application which uses a local .mdf database file for storing some data.

I first tested it on another machine (windows 10, 64bit), which had Visual studio installed, but the application failed to connect to the localDB instance. After installing Microsoft SQL Server 2014 Express (which contains the LocalDB installation packet), it started to work.

I decided to test it also on another machine (Windows 7, no Visual studio, 32bit), where the same problem occurred. Installing the Microsoft SQL Server 2014 Express did not resolve the problem. The returned error message states: “SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified”.

The used connection string in my application is:

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";

I have also checked the following things on the machine with Windows7:

  • I created a LocalDB instance with the command "sqllocaldb.exe create" (creates an instance with the name MSSQLLocalDB) and checked that it was created/is running with "SqlLocalDb info" (returns all running LocalDB instances).
  • Checked that the SQL server's status was set to "running" in SQL Server 2014 Configuration Manager
  • Checked in services.msc that the service "SQL Server" and "SQL Server Browser" were both running.
  • Also tried manually connecting to the LocalDB instance, in SQL Server 2014 Management Studio, by setting the 'server name' field to "(LocalDb)\\MSSQLLocalDB". It connected.

The full stacktrace:

---------------------------
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()

Would anybody know what could be causing the application to work on the Windows 10 machine and to not work on the Windows 7 machine? Both machines have .NET version 4.6.2 installed.

Firstly Go to SQL server and Execute following Command :

SELECT @@SERVERNAME

Now ,Change your local_connection_string [Data Source] ( Mentioned in your code above ) ,with the Above result .

Its all the connectivity issue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM