how to connect sqlserver using powershell

Advertisement
Here we can find the script the way we can connect sqlserver DB using PowerShell.

$con = New-Object System.Data.SqlClient.SqlConnection
$con.ConnectionString = "Server=servername;Database=databasename;User ID=username;Password=password;"
$con.Open()
$sql = "your select query"
$cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$con)
$rdr = $cmd.ExecuteReader()
$test = @()
while($rdr.Read())
{
    $test += ($rdr["fieldname"].ToString())
}
#here we can see the output using

Write-Output $test

SharePoint Online Real Time Training Contact: JLAKSHMITULASI@GMAIL.COM
Advertisements
SHARE

Unknown

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a comment