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

浪子不回头ぞ 提交于 2019-12-20 06:46:46

问题


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


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


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


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:


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



来源:https://stackoverflow.com/questions/16588503/unable-to-connect-to-sql-database-c-vs2012-sql-server-2012

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!