我有我的方法(除非我做了一些愚蠢的事情)昨天完美地导出到 csv。但是,今天我不断收到这个错误:
使用“1”参数调用“填充”的异常:“ExecuteReader:CommandText 属性尚未初始化”
- $rowCount=$SqlAdapter.Fill($dt)
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : NotSpecified: (:) [], MethodInvocationException
- FullyQualifiedErrorId : InvalidOperationException
这是我的语法:
$server = "serverinstance"
$database = "databaseinstance"
$savefile = "C:\Test\sqlresults.csv"
$SelectQuery = "Select Top 1 * from madeuptable"
$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection=New-Object System.Data.SqlClient.SqlConnection($connectionString)
$cmd=$connection.CreateCommand()
$cmd.CommandText = $SelectQuery
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($cmd)
$dt = New-Object System.Data.DataTable
$rowCount=$SqlAdapter.Fill($dt)
if ($rowCount -gt 0) { $dt| Export-Csv $savefile -encoding UTF8 -NoTypeInformation }
$connection.Close()