.net 建立与 SQL Server 的连接时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15924877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error occurred while establishing a connection to SQL Server
提问by HMR
If I have my connection string in the web.config like this (added line feeds for better readability):
如果我在 web.config 中有这样的连接字符串(添加换行符以提高可读性):
<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:
当我在代码中指定连接字符串或使用 ConfigurationManager 获取它时,它不起作用:
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:
从 web.config 获取连接字符串给出了相同的错误:
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings
["conn"].ConnectionString;
The error I am getting is " System.ComponentModel.Win32Exception: The network path was not found"
我得到的错误是“System.ComponentModel.Win32Exception:找不到网络路径”
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)]
与 SQL Server 建立连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供程序:命名管道提供程序,错误:40 - 无法打开与 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).
我需要一些帮助来解决这个问题,几个小时以来一直试图解决这个问题,任何在线建议都是我应该检查网络设置(不适用,因为它连接到 sql server express 的本地实例)。服务器名称(相同的字符串在 VS Express 中有效,但在运行代码中无效)。
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
给组每个人对 mdf 和 ldf 文件的完全权限
In project properties under web I've tried running the project under VS development server and local IIS web server (is IIS Express)
在 web 下的项目属性中,我尝试在 VS 开发服务器和本地 IIS Web 服务器(是 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"
不走运,将代码复制到使用“ASP.NET 空 Web 应用程序”创建的项目时,仍然无法连接到它连接的地方
采纳答案by HMR
The problem was related to the application not running in .net version 4.0
该问题与未在 .net 4.0 版中运行的应用程序有关
According to the following page: http://www.connectionstrings.com/sql-server-2012#sqlconnection
根据以下页面:http: //www.connectionstrings.com/sql-server-2012#sqlconnection
You need .net 4.0 and up to use named pipes:
您需要 .net 4.0 及更高版本才能使用命名管道:
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.
4.0.2 之前的 .NET 框架版本不支持 Server=(localdb) 语法。然而,命名管道连接将用于将 4.0.2 之前的应用程序连接到 LocalDB 实例。
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"/>
回答by Lam Tran Duy
You've properly escaped the db filename but not the datasource, therefore it tries to connect to a datasource named "(localdb)11.0", which (most likely) doesn't exist.
您已经正确地转义了 db 文件名而不是数据源,因此它尝试连接到名为“(localdb)11.0”的数据源,该数据源(很可能)不存在。
Try escaping it properly like this:
尝试像这样正确地转义它:
SqlConnection cn = new SqlConnection("Data Source=(localdb)\v11.0;"+
"Initial Catalog=MyDB; Integrated Security=True; "+
"MultipleActiveResultSets=True; AttachDbFilename=D:\Products.mdf");
回答by Danielle Stone
Instead of using (localdb), have you tried using '.\SQLExpress;' per this page on MSDN? It uses the default instance, which may not be what you need.
您是否尝试过使用 '.\SQLExpress;' 而不是使用 (localdb) 根据MSDN上的这个页面?它使用默认实例,这可能不是您需要的。
it would read like so:
它会这样读:
<add
name="conn"
connectionString="Provider=SqlClient;Data Source=.\SQLExpress; Initial
Catalog=MyDB; Integrated Security=True; MultipleActiveResultSets=True;
AttachDbFilename=D:\Products.mdf"
providerName="System.Data.SqlClient"/>
回答by Rots
Check the version of the reference System.Data between the empty project that works, and your project that does not work. You can use the solution explorer to check this. Are both projects using the same version?
检查有效的空项目和无效的项目之间的参考 System.Data 的版本。您可以使用解决方案资源管理器来检查这一点。两个项目是否使用相同的版本?


回答by Jeff B
Just for chuckles instead of putting (localdb) put a period '.'. So that it would look like
只是为了笑而不是放 (localdb) 放一个句号'.'。所以它看起来像
Data Source=.\v11.0;
I'd be surprised if it works but you can do that in full SQL.
如果它有效,我会感到惊讶,但您可以在完整的 SQL 中做到这一点。
回答by Damith
since this is web site project you need to specify correct .net framework version to build. SqlLocalDb need .net framework 4.
由于这是网站项目,因此您需要指定要构建的正确 .net 框架版本。SqlLocalDb 需要 .net 框架 4。
if you create web project and add web site source files to it, it will work if the .net framework is 4+. When you open web site default target framework or existing dll framework may not be same and may be .net 3.5, that's why you get this error. hope this helps.
如果您创建 Web 项目并向其中添加网站源文件,如果 .net 框架为 4+,它将起作用。当您打开网站默认目标框架或现有 dll 框架可能不相同,可能是 .net 3.5,这就是您收到此错误的原因。希望这可以帮助。


回答by Cowboys
Also... this could be related to your setup in IIS.
另外...这可能与您在 IIS 中的设置有关。
Your website's (session state) could be setup to use SQL Server for your session. Check there too... I just had this issue, and realized our sqlbox connection changed and forgot to update on IIS.
您的网站(会话状态)可以设置为使用 SQL Server 进行会话。检查那里...我刚刚有这个问题,并意识到我们的 sqlbox 连接改变,忘记在 IIS 上更新。
回答by Andrew Keeton
This error showed up after I compressed the C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATAdirectory. Uncompressing it fixed the error.
在我压缩C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA目录后出现此错误。解压缩它修复了错误。

