同时建立到SQL Server的连接时发生错误 [英] error occurred while establishing a connection to SQL Server

查看:263
本文介绍了同时建立到SQL Server的连接时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有我的连接字符串中像这样的web.config中(添加换行符更好的可读性):

 <添加
NAME =conn将
的connectionString =数据源=(的LocalDB)\ 11.0;初始
    目录=的MyDB;集成安全性= TRUE; MultipleActiveResultSets = TRUE;
    AttachDbFilename = D:\ Products.mdf
的providerName =System.Data.SqlClient的/>
 

的连接工作,我可以连接到数据库,在数据库浏览器。

当我指定code ConnectionString中,或使用ConfigurationManager中得到它这是行不通的:

 的SqlCommand命令=新的SqlCommand();
    command.CommandText =选择...;
    command.CommandType = CommandType.Text;
    SqlConnection的CN =新的SqlConnection(数据源=(的LocalDB)\\ 11.0;+
        初始目录=的MyDB;集成安全性= TRUE;+
        MultipleActiveResultSets = TRUE; AttachDbFilename = D:\\ Products.mdf);
    command.Connection = CN;
    cn.Open();
    DataTable中的dataTable =新的DataTable();
    SqlDataReader的阅读器;
    读者= Command.ExecuteReader却();
    dataTable.Load(读卡器);
    cn.Close();
 

获取连接字符串从web.config给出了同样的错误:

 的SqlConnection CN =新的SqlConnection(ConfigurationManager.ConnectionStrings
 [conn将]的ConnectionString。
 

我正的错误是System.ComponentModel.Win32Exception:网络路径找不到

在迹说:

  

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

我需要一些帮助解决这一点,一直在努力解决这个几个小时,所有在线的建议是,我应该检查网络设置(不适用,因为它是连接到SQL Server的EX preSS本地实例)。服务器名称(相同的字符串工作在VS防爆preSS但不能在运行code)。

这可能是一个身份验证问题?如果是的话,为什么联合国信息的错误?

感谢您阅读我的文章,希望你能帮助我与此有关。

更新:

我已经尝试了以下几件事:

由于对MDF和LDF文件Everyone组完全权限

在网络下的项目属性我已经试过运行在VS开发服务器和本地IIS Web服务器项目(是IIS防爆preSS)

没有运气,仍然无法连接的地方复制code到已创建的ASP.NET空Web应用程序

一个项目时,连接完全没有问题
解决方案

该问题相关的应用程序不是在.NET 4.0版运行

根据如下页面: <一href="http://www.connectionstrings.com/sql-server-2012#sqlconnection">http://www.connectionstrings.com/sql-server-2012#sqlconnection

您需要.NET 4.0,并为使用命名管道:<​​/ P>

  

服务器=(的LocalDB)语法不支持.NET框架   4.0.2之前的版本。然而,命名管道的连接将工作   连接pre 4.0.2应用程序的LocalDB实例。

  SqlLocalDB.exe创建MYINSTANCE
SqlLocalDB.exe开始MYINSTANCE
SqlLocalDB.exe信息MYINSTANCE
 

该信息提供了命名管道那么这可以在连接字符串中使用:

 &LT;添加名称=conn将
的providerName =System.Data.SqlClient的
的connectionString =服务器= NP:\\。\管道\的LocalDB#B1704E69 \ TSQL \查询/&GT;
 

If I have my connection string in the web.config like this (added line feeds for better readability):

<add 
name="conn" 
connectionString="Data Source=(localdb)\v11.0; Initial 
    Catalog=MyDB; Integrated Security=True; MultipleActiveResultSets=True; 
    AttachDbFilename=D:\Products.mdf" 
providerName="System.Data.SqlClient"/>

The connection works and I can connect to the database in the database explorer.

When I specify the connectionstring in code or use ConfigurationManager to get it it doesn't work:

    SqlCommand command = new SqlCommand();
    command.CommandText = "select ...";
    command.CommandType = CommandType.Text;
    SqlConnection cn = new SqlConnection("Data Source=(localdb)\\v11.0;"+
        "Initial Catalog=MyDB; Integrated Security=True; "+
        "MultipleActiveResultSets=True; AttachDbFilename=D:\\Products.mdf");
    command.Connection = cn;
    cn.Open();
    DataTable dataTable = new DataTable();
    SqlDataReader reader;
    reader = command.ExecuteReader();
    dataTable.Load(reader);
    cn.Close();

Getting the connection string from the web.config gives the same error:

SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings
 ["conn"].ConnectionString;

The error I am getting is " System.ComponentModel.Win32Exception: The network path was not found"

In the trace is says:

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)]

I need some help solving this, been trying to solve this for hours and any on line suggestion is that I should check network settings (doesn't apply as it's connecting to local instance of sql server express). The server name (same string works in VS Express but not in running code).

Could it be an authentication issue? And if so then why the un informative error?

Thank you for reading my post and hope you can help me with this.

Update:

I've tried the following things:

Given the group everyone full permission on the mdf and ldf files

In project properties under web I've tried running the project under VS development server and local IIS web server (is IIS Express)

No luck, still can't connect where it connects perfectly fine when copying the code to a project that has been created with "ASP.NET empty Web Application"

解决方案

The problem was related to the application not running in .net version 4.0

According to the following page: http://www.connectionstrings.com/sql-server-2012#sqlconnection

You need .net 4.0 and up to use named pipes:

The Server=(localdb) syntax is not supported by .NET framework versions before 4.0.2. However the named pipes connection will work to connect pre 4.0.2 applications to LocalDB instances.

SqlLocalDB.exe create MyInstance
SqlLocalDB.exe start MyInstance
SqlLocalDB.exe info MyInstance

The info provides the named pipe then this can be used in the connection string:

<add name="conn" 
providerName="System.Data.SqlClient" 
connectionString="Server=np:\\.\pipe\LOCALDB#B1704E69\tsql\query"/>

这篇关于同时建立到SQL Server的连接时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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