Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I programmatically instantiate a named LocalDb instance?

I have a WinForms application that is using LocalDb. Of course on my development box, this works fine. When I deploy the application to another box, I am getting the following error message:

"A network-related or instance specific error ocurred while establishing a connection to SQL Server."

I have gone through many of the posts on Stackoverflow on this issue - most seem related to the connection string.

One of the issues I am having is that on my development box, I started a named LocalDb instance. On my install target, I am running the LocalDb installation MSI.

For the installed instance, is there a way to programmatically instantiate a named LocalDb instance or a way to connect to the default LocalDb instance?

Is it possible to programmatically instantiate a named LocalDb instance?

Or can I create a connection string that can be used to open my LocalDb database using (localdb)\v11.0;

I am also using DbContext in the development - again on the development box, LocalDb and DbContext work fine. Does the use of DbContext and EF affect the LocalDb connection string on the target box?

like image 729
Doug Kimzey Avatar asked Nov 16 '12 20:11

Doug Kimzey


People also ask

How do I create a LocalDB instance?

Check and create a LocalDB instance To do that, locate SqllocalDB.exe and run the following command in a command prompt: You will see a list of available LocalDB instances on the server. This command creates an instance of LocalDB named DEVELOPMENT by using SQL Server 2017 binaries and starts the instance.

How do I get LocalDB connection string?

Start LocalDB and connect to LocalDB To connect to a specific database by using the file name, connect using a connection string similar to Server=(LocalDB)\MSSQLLocalDB;Integrated Security=true;AttachDbFileName=D:\Data\MyDB1. mdf .


1 Answers

There are two ways actually:

  1. LocalDBCreateInstance function from LocalDB API, for native C/C++ code and through PInvoke
  2. system.data.localdb / localdbinstances element in your application's app.config (.NET applications only, requires .NET 4.0.2 or later)
like image 182
Krzysztof Kozielczyk Avatar answered Oct 09 '22 14:10

Krzysztof Kozielczyk