问题描述
SQL Server新手-我正在尝试使用SQLCMD实用程序通过PowerShell访问计算机上的localdb \ MSSQLLocalDB服务器.我正在使用PowerShell v5,.NET v5.0,并且在Microsoft SQL Server Management Studio 2014中连接到服务器时,服务器名称为(localdb)\MSSQLLocalDB
.
SQL Server newbie here - I'm trying to access the localdb\MSSQLLocalDB server on my computer through PowerShell with the SQLCMD utility. I'm using PowerShell v5, .NET v5.0, and The server name is (localdb)\MSSQLLocalDB
when I connect to it in Microsoft SQL Server Management Studio 2014.
PS C:\> sqlcmd -S localdb\MSSQLLocalDB
和PS C:\> sqlcmd -S .\localdb\MSSQLLocalDB
导致此错误:
我用SELECT @@ServerName
在Management Studio中查询了服务器名称,并在上述命令中的-S
之后使用了该服务器名称,并得到了相同的错误.
I queried the server name in Management Studio with SELECT @@ServerName
and used that after the -S
in the above command and got the same error.
PS C:\> sqlcmd -S localdb
给出此错误:
其他说明:我可以使用System.Data.SqlClient
和以下连接字符串,在C#控制台应用程序中连接到服务器并使用名为 testdb01 的数据库:
Other notes: I'm able to connect to the server and work with a database named testdb01 in a C# console app using System.Data.SqlClient
with this connection string:
"Data Source=(localdb)\\mssqllocaldb;Initial Catalog=testdb01;Integrated Security=SSPI;"
关于从哪里开始或如何解决这个问题的任何想法?
Any ideas on where to start or how to approach this?
推荐答案
将参数从PowerShell传递到EXE可能很棘手.我认为您需要使用单引号对转义符进行转义,这意味着内容是静态文字字符串或反引号转义字符:
Passing arguments from PowerShell to EXEs can be tricky. I think you need to escape the parens either with single quotes, which means the content is a static literal string, or the backtick escape character:
sqlcmd -S '(localdb)\MSSQLLocalDB' -Q "Select @@servername"
sqlcmd -S `(localdb`)\MSSQLLocalDB -Q "Select @@servername"
甚至使用变量:
PS H:\> $myserver = '(localdb)\MSSQLLocalDB'
PS H:\> sqlcmd -S $myserver -Q "Select @@servername"
此外,如果您使用的是sqlcmd,则可能要看Invoke-Sqlcmd,它对于PowerShell来说更本地化:
Also, if you are using sqlcmd, perhaps look at Invoke-Sqlcmd which is more native to PowerShell:
PS SQLSERVER:\> Invoke-Sqlcmd -Server $myserver -Query "Select @@servername"
这篇关于在PowerShell中通过SQLCMD连接到localdb时出现问题-服务器实例名称是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!