简体   繁体   中英

Connection to SQL Server Express from Visual Studio

I created a database using SQL Server Express from Visual Studio 2012 and I fail to open a connection pointing that database.

I've tried many way like for example:

SqlConnection connectionLocale = new SqlConnection(@"Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\Users\Max\Desktop\presentoir\Application\WpfAppTest\BDD.mdf;Integrated Security=True");

But I keep on getting the same error message :

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) (Microsoft SQL Server, Error: 1326)

As I am a total beginner, even with two days of research, I really can't figure out what the problem is.

I can get a connection using :

SqlConnection connectionLocale = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|BDD.mdf;Integrated Security=True");

But I can't change my database using C# code by connecting like this (I think it creates a temporary database in appdata, and that all the modifications are done in this database).

Every idea helping me to create a connection to my database is welcome.

Okay, a coworker found the solution.

God, it was obvious...

With the @ behind the string, the correct data source was :

@"Data Source=(LocalDB)\v11.0 

and not :

@"Data Source=(LocalDB)\\v11.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM