0

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.

TheAptKid
  • 1,559
  • 3
  • 25
  • 47
  • Are you sure that `path_to_local_DB` is correct? – stuartd Dec 08 '16 at 11:56
  • The .mdf file is inside the application's folder. As you can see, I'm using the GetCurrentDirectory() to get the (dynamic) absolute path to the directory, where it is located. – TheAptKid Dec 08 '16 at 11:58
  • ... and Directory.GetCurrentDirectory() is returning the right value? [The current directory is not always the application directory..](http://stackoverflow.com/questions/15653921/get-current-folder-path) – stuartd Dec 08 '16 at 12:00
  • Never, Never use the mdf file to connect to the database. It is owned by the SQL Server and doesn't allow you to connect. Use the Server Name (or IP) and the name of the database. – jdweng Dec 08 '16 at 12:03
  • @stuartd: The method is returning the correct path. – TheAptKid Dec 08 '16 at 12:15
  • What is this: (LocalDB)\MSSQLLocalDB ? Shuldn't it be .\mssqlocaldb or yourmachine\mssqllocaldb ? And yes, why attach the mdf - it should work without doing that. And are you sure that the instance name is in fact mssqllocaldb ? – agfc Dec 08 '16 at 12:23
  • Have you checked to see if the service is actually running? – Chris Dec 08 '16 at 12:24
  • @Chris: Please see bullet-points 2 and 3. I checked the SQL server's status in SQL Server 2014 Configuration Manager and also checked that two services were running in services.msc. – TheAptKid Dec 08 '16 at 12:28
  • [MSDN](https://msdn.microsoft.com/en-us/library/hh510202.aspx?f=255&MSPPError=-2147217396) suggests that the *easiest way to connect to LocalDB* is via the automatic instance (MSSQLLocalDb) and filename. The connection string in their example matches yours. Can you connect via the named pipe (`SQLLocalDb i MSSqlLocalDb` from command prompt to find)? – David Rushton Dec 08 '16 at 12:56

1 Answers1

0

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 .

UJS
  • 853
  • 1
  • 10
  • 16