Can I programmatically instantiate a named LocalDb instance? - connection-string

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?

There are two ways actually:
LocalDBCreateInstance function from LocalDB API, for native C/C++ code and through PInvoke
system.data.localdb / localdbinstances element in your application's app.config (.NET applications only, requires .NET 4.0.2 or later)

Related

Where to find localdb created?

Currently I was going through Microsoft's ASP.NET Core MVC tutorial and it came to a point where I created a database context class, coming complete with a connection string that I add during the config.
Problem is however, when I check with SQL Server Management Studio, I cannot find the database created; I've logged on to both my local host as well as my PC name (for Windows auth) but the database created doesn't exist on either servers.
MS stated in their docs that when I run the command Update-Database in the PMC it would create it but I cant find it anywhere. I've tried using the name of the server from the connection string (localdb) but I cannot connect because SSMS doesn't recognize the instance.
Any idea why it doesn't show up on the studio but seems to work fine during the application running?
Connection string as follows:
"MvcMovieContext": "Server=(localdb)\\mssqllocaldb;Database=MvcMovieContext-1;Trusted_Connection=True;MultipleActiveResultSets=true"
About the LocalDB location, please, take a look here:
https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-express-localdb?view=sql-server-ver15#:~:text=User%20database%20files%20are%20stored,add%20tables%20in%20Visual%20Studio.
Your LocalDB should be located on C:\Users\<user>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\mssqllocaldb, assuming mssqllocaldb is your instance name, based on the connection strig you have provided.
Also, to connect to your LocalDB, follow the answers given on this post, I guess could be helpful on your case:
How to connect to LocalDB in Visual Studio Server Explorer?

How to connect to localdb in Rider?

Trying to connect to localdb using Database tab in Rider.
I adding SqlServer jTds and Rider shows this error
Connection to SunEngineDb1 failed. [08S01] Network error IOException:
.\pipe\MSSQL$LOCALDB#E65C6709\sql\query (Can not find the specified
file)
What am I missing, and how it can be solved?
It is ASP.NET Core 2.0 project with localdb database on development. If I run it it works perfectly including database connect.
Well, finally we discovered the problem. It is the mistake in the driver we provide. To have this issue fixed urgently, delete the file \config\jdbc-drivers\jdbc-drivers.xml and restart the IDE. Then, please, refresh your driver to 1.3.1.1 or just put [latest] there.

Attach Database as a connection string

So I have this application that I created via VB.NET, Its a Stand Alone Application , During the Development I was setting the app connection string Data source to a Microsoft SQL Server, if the app was only used by me I wouldn't have any problem , my problem is that I want to install it on other Computers , I tried the Attach Database Method , But when I run my application it generates this exception "the user instance login flag is not supported on this version of sql server , the connection will be closed" , I've searched the internet and found out that Attaching Database File is only supported by sql server Express , I have two questions :
If I have SqlLocalDB installed , the attach Database file thing should be working right ?
if i'm wrong , Can you please how can I installed the app with its database without the need to install SQL Server Management studio and attach the database manually so that I could get my app working ?
I believe the answer is here:
http://www.codeproject.com/Questions/304489/Error-msg-the-user-instance-login-flag-is-not-supp
"If you're not using Express with SQL" ... "then you cannot use user instances. You can create the database manually first and set the connection string to point to that database. Remeber to take away the user instance specification from the connection string."
And if you need help with the LocalDB connection string, you can search this page for "LocalDB":
http://www.connectionstrings.com/sql-server/

LocalDB - Error 26 Network related issue

I have a project created while running 2008r2 express and connecting to an instance named "Sqlexpress". I have recently uninstalled Sql server 2008r2 express and started using the recommended LocalDB. All of my projects updated the database successfully and connect flawlessly with the exception of one, my largest project.
The database updated perfectly but now i get a Error 26 - Network or instance related error each time i try to login in. (iterating through a dataset created in the dataset desinger.
Here is my connection string:
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True
I can see the connection in Server Explorer and view table data. Any idea why this wouldn't work at runtime on this one project?
I solved this by opening Dataset.Designer.vb in notepad++ and replacing all instances of ".\SQLEXPRESS" with "(LocalDB\v11.0". I also had to set each instance of "User Instance=True" to False.
I solved the same by replacing the "(localdb)\v11.0" with applicable "MyServerName\SQLServerInstanceName" in connection string. During the installation of SQL Server Express 2014, I chose to keep SQLExpress as instance name.

LightSwitch Beta 1 looking for SQL Express instead of full SQL instance

After resolving issues with RIA installation here, I'm still getting this following error. Not much special - I tried to create a new screen based off of data sources from an existing database in a full SQL 2005 instance
An error occurred while establishing a
connection to SQL Server instance
'.\SQLEXPRESS'. 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)
I've tried to correct the connection string in the, I guess, autogen'd config file. Even though Visual Studio doesn't seem to think that the file is overwritten on rebuild, the error continues to appear.
How can I use LightSwitch with full SQL 2005 rather than SQL Express?
It seems when working Locally, LightSwitch uses SQL Server Express. It puts it's Membership, Roles, and other authentication information here. I don't know any way to get around this locally, but when you go to "Publish" your project, you're given the ability to use a different database/server. You can also use whatever server/database you'd like for your data sources as well.
Hopefully, before V1, they'll allow you to change that. But I haven't found a way yet, without the config being overwritten constantly.
You can't use it with non Express version during development when you want to use LightSwitch to create your data model. If you deploy your project, as mentioned, you can use the created database with full version and change the connection string.
Although I would be cautious using SQL Server 2005 because LightSwitch is using datetime2 which is new in 2008. (maybe some more 2008 specifics as well)
http://lajak.wordpress.com/2011/10/04/lightswitch-change-default-database-instance/
Here, they are telling how to change instance name both in project level and global level.

Resources