如何使用 Invoke-Sqlcmd 连接到 SQL Server LocalDB? [英] How to connect to SQL Server LocalDB using Invoke-Sqlcmd?

查看:14
本文介绍了如何使用 Invoke-Sqlcmd 连接到 SQL Server LocalDB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自 SQLServer 2008 和 SQLServer 2012 的 sqlcmd.exe:

I have sqlcmd.exe from both SQLServer 2008 and SQLServer 2012:

PS C:> Get-Command sqlcmd.exe

Definition
----------
C:Program FilesMicrosoft SQL Server100ToolsBinnSQLCMD.EXE
C:Program FilesMicrosoft SQL Server110ToolsBinnSQLCMD.EXE

通过修改 $env:PATH 我强制使用 SQL Server 2012 中的 sqlcmd.exe:

By modifying $env:PATH i force the use of sqlcmd.exe from SQL Server 2012:

PS C:> $env:PATH = ($env:PATH -split ";" | Where-Object { $_ -notlike "*Microsoft SQL Server100*" }) -join ";"
PS C:> Get-Command sqlcmd.exe

Definition
----------
C:Program FilesMicrosoft SQL Server110ToolsBinnSQLCMD.EXE

LocalDB 的默认实例已启动并运行,并由当前用户拥有:

The default instance of LocalDB is up and running, and owned by the current user:

PS C:> sqllocaldb i v11.0
Name:               v11.0
Version:            11.0.2318.0
Shared name:
Owner:              DOMAINme
Auto-create:        Yes
State:              Running
Last start time:    12/06/13 18:17:57
Instance pipe name: np:\.pipeLOCALDB#08EDBEF0	sqlquery

现在,我可以使用 sqlcmd.exe

PS C:> sqlcmd.exe -S "(localdb)v11.0" -Q "select 1"

-----------
          1

但是当用 Invoke-Sqlcmd 尝试同样的方法时,我得到一个连接错误:

But when trying the same with Invoke-Sqlcmd i get a connection error:

PS C:> Import-Module sqlps
PS C:> Invoke-Sqlcmd -ServerInstance "(localdb)v11.0" -Query "select 1"
Invoke-Sqlcmd : 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

我该怎么做才能使 Invoke-Sqlcmd 连接到 (localdb)v11.0 ?

What can i do to lmake Invoke-Sqlcmd connect to (localdb)v11.0 ?

推荐答案

UPDATE

调用-Sqlcmd 来自 SqlServer模块支持LocalDB:

Invoke-Sqlcmd from the SqlServer module supports LocalDB:

Invoke-Sqlcmd -ServerInstance "(localdb)v11.0" -Query "select 1"
Invoke-Sqlcmd -ConnectionString "Server=(localdb)v11.0; Integrated Security=True" -Query "select 1"

# both will work

这篇关于如何使用 Invoke-Sqlcmd 连接到 SQL Server LocalDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆