Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,
I am using visual studio 2012,framework 4.0,
I am trying to connect to the local db using connection string

SqlConnection con = new SqlConnection(@"Server=(localdb)\v11.0;Integrated Security=true;Database=Practice;");

But I am getting the exception as follows:-

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)

Please advice,Am I missing something?

Regards
Posted
Comments
[no name] 5-May-13 8:14am    
You are incorrectly specifying the server name in your connection string.

First and foremost make sure that the SQL Server services are running. Other reasons could be, incorrect connection string or some Firewall blockage.

Here, have a look at this blog post to troubleshoot the issue : MSDN Blogs: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified[^]

If needed, look at these one:
Exception - error 26 - Error Locating Server Instance Specified[^]
provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance...[^]
"Specified SQL server not found" o..[^]
 
Share this answer
 
Comments
DotnetCoders001 10-May-13 11:20am    
Appriciate your reply.I found a solution as follows :-
Went to Tools->Connect to database, specified server name as (localdb)\v11.0 and then selected the appropriate database from the list and it worked.

But, after closing the application when I reopen my application and reconnect to the local db sometimes it gives error 26,any idea why sometimes it works and sometimes throws this error?
Sandeep Mewara 10-May-13 11:45am    
Hard to say.
The connection string you are using is wrong.
Please try to use the correct one according to your configurations.

For that you have one awesome website - http://www.connectionstrings.com/[^].

Search for the required connection string according to the database and technology.

For Example (SQL Server 2008) - Connection strings for SQL Server 2008[^].
XML
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;


For Local DB, you can just provide a dot "." instead of (localdb)\v11.0.

So, you can try with the below one...
XML
Server=.;Database=Practice;User Id=myUsername;Password=myPassword;

Provide necessary UseName and Passwords.
 
Share this answer
 
1.- Arranca la instancia para poder accesarla desde tu código:
La linea de comando para arrancar la instancia es:
c:\>slqlocaldb start MSSQLLOCALDB

2.- Ejemplo de código para conectar a SQLLocalDb
C#
string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=database;AttachDbFilename=C:\Desarrollo\database.mdf;Integrated Security=True";
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand("select * from usuarios", connection))
using (var adapter = new SqlDataAdapter(command))
{
    var table = new DataTable();
    int count = adapter.Fill(table);
    MessageBox.Show(count.ToString());
}
 
Share this answer
 
My error 26 was solved by updateing the .Net Framework to 4.5
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900