当我从oracle填充数据时,为什么$ dataset.tables [0] powershell为null?

时间:2018-08-06 03:45:54

标签: oracle powershell

我在这里遇到问题,我的代码如下:

# Load the good assembly
Add-Type -Path "C:\oracle\odp.net\managed\common\Oracle.ManagedDataAccess.dll"
# Path to file
$path = "C:\Portal-Statistics\2018\Portalrooms.xlsx"
# Open the Excel document and pull in the 'Play' worksheet
$Excel = New-Object -Com Excel.Application
$Workbook = $Excel.Workbooks.Open($Path)
$page = '_4_docexchange'
$ws = $Workbook.worksheets | where-object {$_.Name -eq $page}
# Delete the current contents of the page
$ws.Cells.Clear() | Out-Null
#Connection string
$compConStr2 = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS= (PROTOCOL=TCP)(HOST=host)(PORT=38370)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=server_name)));User Id=userID;Password=pasword;Provider=OraOLEDB.Oracle"
# Query database
$qry4 = "query database"
# 1st Connection
$oraConn = New-Object System.Data.OleDb.OleDbConnection($compConStr2)
$oraConn.Open()
## Export XLSX
$cmd = New-Object system.Data.OleDb.OleDbCommand($qry4,$oraConn)
$da = New-Object system.Data.OleDb.OleDbDataAdapter($cmd)
$dt = New-Object System.Data.DataTable
$da.Fill($dt) | Out-Null
# Close connection
$oraConn.Close()
$docexList = $dt.Tables[0]
#echo $docexList.Length
$docexList | Export-CSV customers_with_orders.csv

当我导出到.csv文件时,我的错误是$docexList为空。我该如何解决?

1 个答案:

答案 0 :(得分:0)

我总是使用Write-Host行对NULL值错误进行故障排除,这些行从上到下逐步输出我的相关变量。我先检查一下:

if ($cmd -eq $Null) {
    Write-Host "Value is null."
} Else {
    Write-Host "Value is NOT null."
}

如果不是,则将脚本下移至$ da,$ dt,...并相应地更改if (...)。最终,您会发现哪一行返回空值。找到该行后,可能是某个地方的语法错误或逻辑错误。

此外,在此行$da.Fill($dt) | Out-Null上,您可以尝试删除| Out-Null。我知道可以在那里删除它打印的烦人的数字,但是我似乎隐约记得我前一段时间也遇到了问题。值得一试。