読者です 読者をやめる 読者になる 読者になる

# SQLServerからデータの取得
# 接続型と非接続型でやってみた
# 接続文字列
$ConnectionString 	=	"Data Source=localhost;Initial Catalog=master;Integrated
 Security=SSPI;"
#SQL文
$CommandText		=	"select name from sys.databases order by name"

$cn = New-Object System.Data.SqlClient.SqlConnection($ConnectionString)
$cn.Open()
$cmd = New-Object System.Data.SqlClient.SqlCommand($CommandText,$cn)

Write-Host "DataReader 接続型"
$dr = $cmd.ExecuteReader()
$dr | ForEach-Object {
    Write-Host $_.Item("name")
}
$dr.Close()


Write-Host "DataAdapter 非接続型"
$dt = New-Object System.Data.DataTable
$da = New-Object System.Data.SqlClient.SqlDataAdapter($cmd)
$da.Fill($dt)
$da.Dispose()
$dt | ForEach-Object {
    Write-Host $_.Item("name")
}
$cn.Close()