2016-07-24 194 views
13

我目前正在学习SQL Server,并且在使用sqlcmd连接到本地数据库时遇到问题。无法通过sqlcmd连接到LocalDB

当我在下面到PowerShell的类型:

sqlcmd -S "(localdb)\mssqllocaldb" 

我收到以下错误信息:

Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : SQL Server Network Interfaces: The system cannot find the file specified.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.

我可以连接到本地数据库,如果我使用SQL Server Management Studio中。如果我使用实例名称管道,我也可以连接到本地数据库。

我有一个干净的安装Windows 10 64,Visual Studio的社区,和SQL Server Management Studio中2016年

+0

是'(的LocalDB)'你的主机名和'mssqllocaldb'数据库实例的名称?你[启用TCP协议](https://technet.microsoft.com/en-us/library/hh231672%28v=sql.110%29.aspx)? [参考文献](https://msdn.microsoft.com/de-ch/library/ms188247.aspx)。 –

+0

使用'Invoke-Sqlcmd'为我解决了这个问题。在这里回答的建议:http://stackoverflow.com/questions/34457048/trouble-connecting-to-localdb-via-sqlcmd-in-powershell-what-is-server-instance –

+0

具有相同的问题,仍然没有回答 –

回答

1

尝试

sqlcmd -S ".\mssqllocaldb" 

sqlcmd -S ".\mssqllocaldb -uYOUR_User -pYOUR_PASSWORD" 
+0

并且还要确保服务已启动 –

7

有一个driver bug ODBC驱动器13.安装ODBC驱动程序13.1更新将解决此问题。

您可以download the driver here

之后,你应该能够在cmd中或PowerShell来使用:

sqlcmd -S "(localdb)\MSSQLLocalDB" -i C:\path\to\file.sql