Cannot open LocalDB

JonJacobs

Active member
Joined
Jul 28, 2022
Messages
35
Programming Experience
10+
On my old win8.1 computer I could do just about anything database.
On my new win11 computer I wrote a little program to open an instance of SQL Server Express.
I went to command prompt to make sure it was running. I typed sqllocaldb start. The response was LocalDB instance "MSSQLLocalDB started.
I launched my little winforms program and got an exception on myConn.Open(); Here is the most relevant code in the Form1.cs file:
private SqlConnection myConn;
private string ConStr = @"Data Source= .\SQLExpress; User Instance=true; Integrated Security=true; Initial Catalog=master";


private void bnOpen_Click(object sender, EventArgs e)
{
myConn.ConnectionString = ConStr;
try
{
myConn.Open();
}
catch (Exception ex)
{
string msg = ex.ToString();
MessageBox.Show(msg);
}
}
Here is the exception string, msg:
System.Data.SqlClient.SqlException (0x80131904): 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.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at CreateDB.Form1.bnOpen_Click(Object sender, EventArgs e) in C:\Users\jonqj\source\CreateDB\CreateDB\Form1.cs:line 73
ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:-1,State:0,Class:20
 
Solution
You're not connecting to the right instance

If you make a database at the command line like:

sqllocaldb create HelloWorldYeah

Then your connection string would be:

Data Source=(localdb)\HelloWorldYeah;...
See:
 
Try temporarily disabling your antivirus and/or firewall. If things start working after doing that, re-enable the AV or firewall and start going through the documentation of the AV or firewall to see how to make an exception for SQL Express' process and/or ports.
 
Also it might not be the AV is blocking SQL Express. It might be the AV is blocking your app. Some AV looks to see if an app is digitally signed, and if not it pretty much runs the code essentially in a sandbox where it has limited ability to communicate out. You might need to tell the AV to always allow code that you are developing within a particular subdirectory.
 
Thank you.
Yes, I have seen AV block an app I coded. In those cases it doesn't let me run the app at all until I grant permission. In this case, the app is not prevented from launching, just that an exception is thrown when I try to open the LocalDB instance, when I click the button (creatively named 'Open') on the form to do so.
 
Additional note: Under Windows Security the setting for app and browser control is turned off. That is why the app could be launched, I believe.
 
You're not connecting to the right instance

If you make a database at the command line like:

sqllocaldb create HelloWorldYeah

Then your connection string would be:

Data Source=(localdb)\HelloWorldYeah;...
 
Solution
Thank you, @cjard. I am deliberately targeting the default instance, MSSQLLocalDB, and do not want another (named) instance. I made sure the default instance was started just before launching the app.
 
You're not connecting to the right instance

If you make a database at the command line like:

sqllocaldb create HelloWorldYeah

Then your connection string would be:

Data Source=(localdb)\HelloWorldYeah;...

You were right! I don't know why the default instance didn't work, but since nothing else handled the issue, I decided to try this anyway. It Worked!
 
You're not connecting to the right instance

If you make a database at the command line like:

sqllocaldb create HelloWorldYeah

Then your connection string would be:

Data Source=(localdb)\HelloWorldYeah;...

Your post is the answer! I put a like on it, but I would like to mark it as the answer. I don't see a way to mark your post as the answer. Do you know how I can do that? Thanks.
 
Click check mark in a circle on the right side of the the post that you want to mark as the solution.
 
Back
Top Bottom