必须有一种简单的方法来执行此操作。我有一个连接到数据库的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()

最佳答案

我认为应该这样做:

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

关于powershell - 使用Powershell将数据表另存为csv,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10149910/

10-13 00:13
查看更多