Advertisement
ibi

sqlserver ado connection with powershell

ibi
Feb 11th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $sqlcon = New-Object System.Data.SqlClient.SqlConnection
  2. $sqlcon.ConnectionString = "Data Source=mysqlserver\myinstance1; Initial Catalog=blah; uid=hans; pwd=wurst" # integratedsecurity=true for domainusers
  3. $sqlcmd = New-Object System.Data.SqlClient.SqlCommand
  4. $sqlcmd.Connection = $sqlcon
  5. $sqlcmd.CommandText = "select top 20 rowname FROM tablename"
  6. $sqlcon.Open()
  7. $sqlds = New-Object System.Data.DataSet
  8. $sqlda = New-Object System.Data.SqlClient.SqlDataAdapter ($sqlcmd)
  9. [void]$sqlda.fill($sqlds)
  10. $sqlcon.Close()
  11. $sqlds.Tables
  12. write-host "-- we have" $sqlds.Tables[0].row.count "rows"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement