简体   繁体   English

无法连接到SQL数据库 - C#,VS2012,SQL Server 2012

[英]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. 我在Windows 7 64位PC上安装了SQL Server 2005,2008和2012。

在此输入图像描述


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. 以下是VS2012数据库资源管理器窗口中显示的内容。 This is a SQL Server 2012 database 这是一个SQL Server 2012数据库

在此输入图像描述


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(); 如上所述,cn.Open(); line is what throws the error message shown below line是什么抛出下面显示的错误消息

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与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. 验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)

Data Source=adamssqlserver

is wrong, should be: 错了,应该是:

Data Source=lpc193\adamssqlserver

This can be seen in the Server Explorer screenshot you attached. 这可以在您附加的Server Explorer截图中看到。 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 您的连接字符串正在寻找计算机调用“adamssqlserver”,而您的数据库是计算机上的命名实例,称为“lpc193”,因此被称为lpc193 \\ adamssqlserver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM