当我希望将结果保存到csv formet下的文件夹中时,我具有以下脚本来运行脚本并返回数据集:
$connectionString = "Data Source=$sqlserver; User ID = $username; Password = $pws;Initial Catalog=$database;"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$query = “SELECT TOP 5 * FROM Test”
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
$result |export-csv c:\temp\Test.csv -notypeinformation
$connection.Close()
它会生成csv文件,但是csv文件中包含的内容是数据集每一行的字段计数,而不是数据集本身。有人知道我的脚本出了什么问题吗?
答案 0 :(得分:0)
尝试将$ result变量从$ command.ExecuteReader()更改为invoke-sqlcmd
$result = invoke-sqlcmd -query $query -serverinstance $sqlserver -database $database