1

So I am trying to connect a local file database when I debug the program or use it anywhere on my dev machine, it works absolutely fine. Whenever I try to take the files and run it on a different machine I get

Error 50 cannot connect to the localdb: local database runtime error occurred, The specified LocalDB instance does not exist

Now my issue is, I kind of know what is causing the issue, I just dont know how to resolve it.

I have installed SQL Server express 2016 on my testing machine (the one it doesnt work on) and the localDb never gets initialized.

On my Dev machine, I can goto "C:\Program Files\Microsoft SQL Server\130\LocalDB"

However on my test machine I only have "C:\Program Files\Microsoft SQL Server\130\" No LocalDB.

I believe my issue is that I am never actually creating the sql server instance rather just calling the file (see code below) but I dont know how to initalize a local instance of an sql server. I have searched and tried different methods but nothing is working for me.

    public SqlConnection connectionOpen()
    {
        string databaseDirectory = Convert.ToString(Directory.GetCurrentDirectory() + @"\gameDatabase.mdf");
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = @"Data Source =(LocalDB)\MSSQLLocalDB; AttachDbFilename ="+databaseDirectory +"; Integrated Security = True"; 
        conn.Open();
        return conn;
    }
    public void connectionClose()
    {
        string databaseDirectory = Convert.ToString(Directory.GetCurrentDirectory() + @"\gameDatabase.mdf");
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = @"Data Source =(LocalDB)\MSSQLLocalDB; AttachDbFilename ="+databaseDirectory + "; Integrated Security = True";
        conn.Close();
    }

As mentioned this works flawlessly on my local dev machine, no matter where the exe and mdf files are (as long as they're in the same directory as thats how im finding the mdf file in code), but as soon as I try to run it on a different machine I get a connection error.

Any help would be really appreciated! I am pulling my hair out!

  • You can create, start, stop and delete instances via the [command line](https://msdn.microsoft.com/en-gb/library/hh247716.aspx?f=255&MSPPError=-2147217396). But this will only work if localDB is installed on the testing box. – David Rushton Nov 24 '16 at 11:39
  • Sorry I am not all that clued up with SQL, I am trying to learn, my database is called "gameDatabase.mdf" and the table is called "gameDb" does it matter what I call the instance when creating using the command line? – MagicalMidge Nov 24 '16 at 11:46
  • No need to apologise. The instance name needs to match your connection string. In this case it's `(LocalDB)\MSSQLLocalDB`. That is the default instance. It should always be available. That suggests you need to [download](https://msdn.microsoft.com/en-us/library/hh510202.aspx?f=255&MSPPError=-2147217396) and install LocalDB. LocalDb provides the engine, the .mdf has the structure and content. – David Rushton Nov 24 '16 at 11:50
  • I absolutely love you!!! I really dont like asking for help unless I feel I really need it, 2 days I have been trying to figure this out for! So I had installed MySQL Express 2016 but apparently localDb wasnt a default option. Honestly thank you very much my man <3 – MagicalMidge Nov 24 '16 at 11:57
  • Of course! I'd forgotten that LocalDb does not have its own installer. It comes bundled with Express or [SSDT](https://msdn.microsoft.com/en-us/library/mt204009.aspx?f=255&MSPPError=-2147217396). That must have been driving you crazy. – David Rushton Nov 24 '16 at 12:28
  • Localdb has it's own installer. Here it is : https://www.microsoft.com/en-us/download/details.aspx?id=29062 Just check the `SqlLoaclDb.MSI` while downloading. – Atanu Roy Nov 24 '16 at 13:45

2 Answers2

0

For anyone that may come across this I hope I can inform you of the "fix" that resolved it for me.

From user "Destination-data"
No need to apologise. The instance name needs to match your connection string. In this case it's (LocalDB)\MSSQLLocalDB. That is the default instance. It should always be available. That suggests you need to download and install LocalDB. LocalDb provides the engine, the .mdf has the structure and content.

Basically I hadnt installed the LocalDB on my test machine, I was under the impression that it came bundled with the basic version of SQL Server Express 2016, this wasnt the case and is a separate download.

You need to download the SQL Server Express 2016 downloader, use custom install and select "LocalDb" from there, it will download SqlLoaclDb.MSI Run this, and then try your program again, it should work.

0

you maybe missing the database in the other test machine/environment, can create the MSSqlLocalDB with the cmd> sqllocaldb create MsSsqlLocalDB and then change the create switch to start and rerun the command.


To begin - there are 4 issues that could be causing the common LocalDb SqlExpress Sql Server connectivity errors SQL Network Interfaces, error: 50 - Local Database Runtime error occurred, before you begin you need to rename the v11 or v12 to (localdb)\mssqllocaldb

I found that the simplest is to do the below - I have attached the pics and steps for help.

First verify which instance you have installed, you can do this by checking the registry and by running cmd

  1. cmd> Sqllocaldb.exe i
  2. cmd> Sqllocaldb.exe s "whicheverVersionYouWantFromListBefore" if this step fails, you can delete with option d cmd> Sqllocaldb.exe d "someDb"
  3. cmd> Sqllocaldb.exe c "createSomeNewDbIfyouWantDb"
  4. cmd> Sqllocaldb.exe start "createSomeNewDbIfyouWantDb"

SqlLOCALDb_edited.png

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Transformer
  • 6,963
  • 2
  • 26
  • 52