简体   繁体   English

无法连接到本地SQL数据库

[英]Can't Connect to Local SQL Database

This: 这个:

SqlConnection myConnection = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename='C:\\Users\\Joe\\Documents\\Visual Studio 2012\\Projects\\FileIO\\FileIO\\Database.mdf';Integrated Security=True");
try
{
   myConnection.Open();
}
catch (Exception e)
{
   Console.WriteLine(e.ToString());
}

Returns this error: 返回此错误:

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. System.Data.dll中发生了类型为'System.Data.SqlClient.SqlException'的第一次机会异常System.Data.SqlClient.SqlException:建立与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) (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

Any Ideas? 有任何想法吗?

You're missing the double backslash on the data source. 您在数据源上缺少双反斜杠。 It's reading the \\v as a vertical tab. 它正在读取\\v作为垂直选项卡。

Or you can use a leading @ before the whole string to treat it as a verbatim string (if you do this then you'll need to clear out the extra backslashes on your filepath). 或者,您可以在整个字符串之前使用前导@ ,将其视为逐字字符串(如果这样做,则需要清除文件路径上的多余反斜杠)。

(Copied from error occurred while establishing a connection to SQL Server ) (从建立与SQL Server的连接时发生的错误中复制)

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. 4.0.2之前的.NET框架版本不支持Server =(localdb)语法。 However the named pipes connection will work to connect pre 4.0.2 applications to LocalDB instances. 但是,命名管道连接可以将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"/>

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

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