0

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)

1
  • string connectionString = "Data Source=lpc192\adamssqlserver;Database=master;Integrated Security=True;"; Commented May 16, 2013 at 13:19

1 Answer 1

2
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

Sign up to request clarification or add additional context in comments.

4 Comments

Ah! I had tried that before but only with a single slash... then gave up on that thought. I used two back slashes this time (to escape) and now it works! thank you very much!!!
Ah right, you can get around that by prefixing your connectionstring with an @ symbol, please see: dotnetperls.com/string-literal
No worries. Please feel free to accept the answer in case any body else has the same problem.
Will do! It won't let me accept for 4 more minutes though, lol. Also would you change the lpc192 to lpc193, no big deal but very slight difference :) thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.