Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

.net - error occurred while establishing a connection to SQL Server

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"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...