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

查看:436
本文介绍了如何使用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 Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.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 Server\100\*" }) -join ";"
PS C:\> Get-Command sqlcmd.exe

Definition
----------
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.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:              DOMAIN\me
Auto-create:        Yes
State:              Running
Last start time:    12/06/13 18:17:57
Instance pipe name: np:\\.\pipe\LOCALDB#08EDBEF0\tsql\query

现在,我可以使用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 ?

推荐答案

这是在不利条件下对我有用的代码(请参阅代码后的我的评论).我怀疑较简单的代码可能会在更常见的环境中工作,但我没有深入研究它.

This is code that works for me under adverse conditions (see my comments just after the code). I suspect that simpler code may work in a more common environment, but I haven't dug into it.

几分钟后实例管道超时.如果可以使用(localdb)\ instanceName 进行连接,则效果更好,因为这些连接似乎没有超时.

The instance pipe times out after a few minutes. You're better off if you can connect using (localdb)\instanceName, because those connections don't seem to time out.

function Get-InstancePipeName ([string] $localDbName)
{
  while (!($pipeName = ((sqllocaldb info $localDbName) -match 'instance pipe name').Split(':', 2)[1].Trim()))
  {
    sqllocaldb start $localDbName | Out-Null
  }
  return $pipeName
}

$scsb   = New-Object System.Data.SqlClient.SqlConnectionStringBuilder
$scsb.psbase.DataSource = Get-InstancePipeName localDbName # <== put your db name here
$sc     = New-Object System.Data.SqlClient.SqlConnection $scsb.ConnectionString

$smoSc  = New-Object Microsoft.SqlServer.Management.Common.ServerConnection $sc
$smoSvr = New-Object Microsoft.SqlServer.Management.Smo.Server $smoSc
Invoke-Sqlcmd -ServerInstance $smoSvr -Query 'select 1'

由于当前我无法控制的原因,运行它的执行环境是不寻常的.这是一个具有不完整会话上下文的远程执行环境.另外,我必须重新定义USERPROFILE才能解决其他一些问题.

For reasons currently outside my control, the execution environment where this runs is unusual. It's a remote execution environment with an incomplete session context. Also, I had to redefine USERPROFILE to work around some other issues.

[稍后我最近发现延长超时的一种方法-我必须在第二次添加一个RECONFIGURE sp_configure并(建议)停止并启动localdb以使其生效)]

[later edit: I recently found a way to extend the timeout - I had to add a RECONFIGURE after the 2nd sp_configure and (as recommended) stop and start the localdb to get it to take effect)]

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

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