How to connect to SQL Server LocalDB using Invoke-Sqlcmd?

扶醉桌前 提交于 2019-12-04 02:58:24
user2681363

Got this from a couple other sources, seems to work so far.

JBs Powershell

and

How can I run PowerShell with the .NET 4 runtime?

Another way of making PowerShell and LocalDB play nice is to make PowerShell aware of DOTNET 4.0.3. This can be done by creating a file called "powershell.exe.config" in the C:\Windows\System32\WindowsPowerShell\v1.0 . The file should contain the following:

<?xml version="1.0"?>
<configuration> 
     <startup useLegacyV2RuntimeActivationPolicy="true"> 
          <supportedRuntime version="v4.0.30319"/> 
          <supportedRuntime version="v2.0.50727"/> 
     </startup> 
</configuration>

Be aware that this not an officially supported way of using PowerShell, so it might break other stuff ...

unbob

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.

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'

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.

[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)]

I'm guessing that invoke-sqlcmd doesn't know what "(localdb)" is. Try using localhost instead.

I have been doing this at work recently and had some initial troubles connecting to a local Database. To get it to work, I ran the following code;

C:\> Import-Module sqlps -DisableNameChecking
SQLSERVER\:> cd ".\SQL\$(hostname)"
SQLSERVER\:> Invoke-Sqlcmd -Username "user" -Password "pass" -Database "databasename" -Query "foobar"

This worked for me and I was able to query the database. Obviously, change the Username, Password and Database parameter details to whatever the name of your database on the SQL Instance is called.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!