无法连接到SQL数据库 - C#,VS2012,SQL Server 2012中 [英] Unable to connect to SQL database - C#, VS2012, SQL Server 2012

查看:496
本文介绍了无法连接到SQL数据库 - C#,VS2012,SQL Server 2012中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有SQL Server 2005中,2008年和2012年我的Windows 7 64位PC上安装








这是我的配置管理器,我看到Agent停止......不知道这是必需的。我这样的大小显示出来已经把它分解为两个大的图像





在这里输入的形象描述






这里是在VS2012数据库资源管理器窗口中。这是一个SQL Server数据库2012








这里是我的代码

 字符串selectSql =从任务中选择*; 

字符串的connectionString =数据源= adamssqlserver;数据库=主;集成安全性= TRUE;;

使用(VAR CN =新的SqlConnection(的connectionString))使用(VAR CMD =新的SqlCommand(selectSql,CN))

{
cn.Open() ; //这是引发错误消息的行。使用
(VAR读卡器= cmd.ExecuteReader())
{
//做些什么
}
}

<强>如上所述,cn.Open();线是抛出如下所示的错误信息




一个网络相关的或发生的实例特定的错误,而
与SQL Server建立连接。服务器没有被发现或
无法访问。验证实例名称是否正确,以及
SQL Server配置为允许远程连接。 (provider:命名
管道提供程序,错误:40 - 无法打开到SQL Server的连接)



解决方案

 数据源= adamssqlserver 

是错了,应该是:

 数据源= lpc193\adamssqlserver 

这可在您连接服务器资源管理器截图中可以看到。您的连接字符串正在寻找一台电脑称为adamssqlserver,而你的数据库是你的电脑,这就是所谓的lpc193关于命名实例,所以称呼为lpc193\adamssqlserver


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)

解决方案

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

这篇关于无法连接到SQL数据库 - C#,VS2012,SQL Server 2012中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆