error occurred while establishing a connection to SQL Server

戏子无情 提交于 2019-12-18 05:42:14

问题


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"


回答1:


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.

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:


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



回答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.

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:


Just for chuckles instead of putting (localdb) put a period '.'. So that it would look like

Data Source=.\v11.0;

I'd be surprised if it works but you can do that in full SQL.




回答5:


since this is web site project you need to specify correct .net framework version to build. SqlLocalDb need .net framework 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.




回答6:


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?




回答7:


Also... this could be related to your setup in 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.




回答8:


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



来源:https://stackoverflow.com/questions/15924877/error-occurred-while-establishing-a-connection-to-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!