adam
adam

Reputation: 2970

Unable to connect to SQL database - C#, VS2012, SQL Server 2012

I have SQL Server 2005, 2008 and 2012 installed on my Windows 7 64bit PC.

enter image description here


Here is my Configuration Manager, and I do see that the Agent is Stopped... not sure if this is required. I've split it out into two images so the size shows up larger

enter image description here

enter image description here


Here is what is shown in the VS2012 Database Explorer window. This is a SQL Server 2012 database

enter image description here


Here is my code

string selectSql = "select * from Tasks";

string connectionString = "Data Source=adamssqlserver;Database=master;Integrated Security=True;";

using (var cn = new SqlConnection(connectionString))
using (var cmd = new SqlCommand(selectSql, cn))
{
    cn.Open(); // this is the line that throws the error message.
    using (var reader = cmd.ExecuteReader())
    {
            //do something
    }
}

As noted, the cn.Open(); line is what throws the error message shown below

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Upvotes: 0

Views: 3740

Answers (1)

KingCronus
KingCronus

Reputation: 4529

Data Source=adamssqlserver

is wrong, should be:

Data Source=lpc193\adamssqlserver

This can be seen in the Server Explorer screenshot you attached. Your connection string is looking for a computer calls "adamssqlserver", whereas your database is a named instance on your computer, which is called "lpc193", so is addressed as lpc193\adamssqlserver

Upvotes: 2

Related Questions