11

必须有一个简单的方法来做到这一点。我有一个连接到数据库的 Powershell 脚本,我想将生成的数据表保存为 CSV。这是到目前为止的代码:

$connString = "Provider=msdaora;Data Source=MyDatabase;User Id=test;Password=test    
$qry = "select * from employees"
$OLEDBConn = New-Object System.Data.OleDb.OleDbConnection($connString)            
$OLEDBConn.open()            
$readcmd = New-Object system.Data.OleDb.OleDbCommand
$readcmd.Connection = $OLEDBConn
$readcmd.CommandTimeout = '300'
$readcmd.CommandText = $qry
$da = New-Object system.Data.OleDb.OleDbDataAdapter($readcmd)            
$dt = New-Object system.Data.datatable            
[void]$da.fill($dt)            
$OLEDBConn.close()
4

1 回答 1

28

我认为应该这样做:

$ds = New-Object System.Data.DataSet
$da.Fill($ds) >$null| Out-Null
$ds.Tables[0] | export-csv tofile.csv -notypeinformation
于 2012-04-14T00:28:56.903 回答