无法连接到 SQL 数据库 - C#、VS2012、SQL Server 2012 [英] Unable to connect to SQL database - C#, VS2012, SQL Server 2012
问题描述
我的 Windows 7 64 位 PC 上安装了 SQL Server 2005、2008 和 2012.
这是我的配置管理器,我确实看到代理已停止...不确定是否需要这样做.我把它分成两张图片,所以尺寸显示得更大
这是 VS2012 数据库资源管理器窗口中显示的内容.这是一个 SQL Server 2012 数据库
这是我的代码
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
}
}
如前所述,cn.Open();行是引发下面显示的错误消息的原因
发生与网络相关或特定于实例的错误建立与 SQL Server 的连接.未找到服务器或无法访问.验证实例名称是否正确并且SQL Server 配置为允许远程连接.(提供者:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接)
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)
推荐答案
Data Source=adamssqlserver
错了,应该是:
Data Source=lpc193adamssqlserver
这可以在您附加的服务器资源管理器屏幕截图中看到.您的连接字符串正在寻找名为adamssqlserver"的计算机,而您的数据库是您计算机上的命名实例,称为lpc193",因此寻址为 lpc193adamssqlserver
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 lpc193adamssqlserver
这篇关于无法连接到 SQL 数据库 - C#、VS2012、SQL Server 2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!