在建立到SQL Server的连接时发生错误

时间:2021-06-07 18:26:20

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:

当我在代码中指定connectionstring或使用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”创建的项目中,仍然无法很好地连接。净空Web应用程序”

8 个解决方案

#1


13  

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 filename,而不是datasource,因此它尝试连接到名为“(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");

#2


8  

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框架版本不支持服务器=(localdb)语法。但是,命名管道连接可以将pre 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"/>

#3


3  

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.

您是否尝试过在MSDN上使用“.\SQLExpress;”而不是使用(localdb) ?它使用默认实例,这可能不是您需要的。

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"/>

#4


1  

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。

#5


1  

since this is web site project you need to specify correct .net framework version to build. SqlLocalDb need .net framework 4.

由于这是web站点项目,您需要指定要构建的正确的.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项目并向其添加web站点源文件,那么如果.net框架是4+,那么它将会工作。当您打开web站点的默认目标框架或现有的dll框架时,可能不一样,可能是。net 3.5,这就是为什么会出现这个错误。希望这个有帮助。

在建立到SQL Server的连接时发生错误

#6


1  

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?

检查参考系统的版本。工作的空项目和不工作的项目之间的数据。您可以使用解决方案资源管理器来检查这个。两个项目都使用相同的版本吗?

在建立到SQL Server的连接时发生错误

#7


0  

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。

#8


0  

This error showed up after I compressed the C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA directory. Uncompressing it fixed the error.

这个错误出现在我压缩C:\Program Files\Microsoft SQL Server \ MSSQL11。MSSQLSERVER \该\数据目录。解压缩它修正了错误。

#1


13  

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 filename,而不是datasource,因此它尝试连接到名为“(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");

#2


8  

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框架版本不支持服务器=(localdb)语法。但是,命名管道连接可以将pre 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"/>

#3


3  

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.

您是否尝试过在MSDN上使用“.\SQLExpress;”而不是使用(localdb) ?它使用默认实例,这可能不是您需要的。

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"/>

#4


1  

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。

#5


1  

since this is web site project you need to specify correct .net framework version to build. SqlLocalDb need .net framework 4.

由于这是web站点项目,您需要指定要构建的正确的.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项目并向其添加web站点源文件,那么如果.net框架是4+,那么它将会工作。当您打开web站点的默认目标框架或现有的dll框架时,可能不一样,可能是。net 3.5,这就是为什么会出现这个错误。希望这个有帮助。

在建立到SQL Server的连接时发生错误

#6


1  

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?

检查参考系统的版本。工作的空项目和不工作的项目之间的数据。您可以使用解决方案资源管理器来检查这个。两个项目都使用相同的版本吗?

在建立到SQL Server的连接时发生错误

#7


0  

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。

#8


0  

This error showed up after I compressed the C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA directory. Uncompressing it fixed the error.

这个错误出现在我压缩C:\Program Files\Microsoft SQL Server \ MSSQL11。MSSQLSERVER \该\数据目录。解压缩它修正了错误。