简体   繁体   English

从Visual Studio连接到SQL Server Express

[英]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. 我使用Visual Studio 2012中的SQL Server Express创建了数据库,但无法打开指向该数据库的连接。

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. 建立与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. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 1326) (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)(Microsoft SQL Server,错误: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). 但是我不能通过这样的连接使用C#代码更改数据库(我认为它在appdata中创建了一个临时数据库,并且所有修改都在该数据库中完成)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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