[英]Oracle query and output to CSV
I would like to seek for advice on how am I going to output the oracle query to csv with same wide format output from oracle query. 我想寻求有关如何将oracle查询输出到csv以及从oracle查询输出相同格式的建议。 below is the excerpt from my code 以下是我的代码摘录
[System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient") | Out-Null
$connection = New-Object System.Data.OracleClient.OracleConnection($connectionString)
$command = new-Object System.Data.OracleClient.OracleCommand($queryString, $connection)
$connection.Open()
$users = $command.ExecuteReader()
$Counter = $users.FieldCount
$dataset =
@( while ($users.Read()) {
for ($i = 0; $i -lt $Counter; $i++) {
New-Object PsObject -Property @{ Key = $users.GetName($i)
Value = $users.GetValue($i)
}
}
})
$connection.Close()
Output: 输出:
PS C:\WINDOWS\system32> $dataset
Key Value
--- -----
PRCDNAME A123rw-T03
ASSY_PART1 wrwrwee-A03
ASSY_PART2 wrewrwe-A0CAD
ASSY_PART3 wer
ASSY_PART4
ASSY_PART5
ASSY_PART6
OUTPN_1 rgsrggs-3
OUTPN_2
OUTPN_3
OUTPN_4
OUTPN_5
OUTPN_6
OUTPN_7
OUTPN_1_ALLOC1
I want to format it as below. 我想格式化如下。 basically I need the Key column as the header 基本上我需要Key列作为标题
PRCDNAME,ASSY_PART1,ASSY_PART2 ,......,
A123rw-T03,wrwrwee-A03,wrewrwe-A0CAD,....,
Create a hashtable from your object list: 从对象列表创建哈希表:
$ht = @{}
$dataset | ForEach-Object { $ht[$_.Key] = $_.Value }
Then make an object from that hashtable and export it to a CSV: 然后从该哈希表中创建一个对象并将其导出到CSV:
New-Object -Type PSObject -Property $ht | Export-Csv 'C:\path\to\output.csv' -NoType
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.