Click here to Skip to main content
12,395,645 members (82,118 online)
Rate this:
Please Sign up or sign in to vote.
See more: VS2012
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?

Posted 4-May-13 23:55pm
ThePhantomUpvoter 5-May-13 8:14am
You are incorrectly specifying the server name in your connection string.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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..[^]
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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 -[^].

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

For Example (SQL Server 2008) - Connection strings for SQL Server 2008[^].
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...
Server=.;Database=Practice;User Id=myUsername;Password=myPassword;
Provide necessary UseName and Passwords.
Rate this: bad
Please Sign up or sign in to vote.

Solution 6

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
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);
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

My error 26 was solved by updateing the .Net Framework to 4.5

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 23 Aug 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100