使用Powershell将数据表保存为csv

时间:2012-04-14 00:21:11

标签: powershell csv datatable

必须有一种简单的方法来做到这一点。我有一个连接到数据库的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()

1 个答案:

答案 0 :(得分:19)

我应该这样做:

$ds = New-Object System.Data.DataSet
$da.Fill($ds) >$null| Out-Null
$ds.Tables[0] | export-csv tofile.csv -notypeinformation