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

后端 未结 4 2033
猫巷女王i
猫巷女王i 2021-02-20 11:39

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

PS C:\\> Get-Command sqlcmd.exe

Definition
----------
C:\\Program Files\\Microsof         


        
4条回答
  •  感动是毒
    2021-02-20 11:54

    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.

提交回复
热议问题