我有一个 powershell 脚本,它接受我计划用来执行存储过程的用户输入。sproc 接受多个参数来自定义返回的数据。但是,当我运行此 powershell 代码时,出现以下错误:
Cannot index into a null array.
At C:\Report.ps1:119 char:42
+ foreach ($Row in $global:DataSet.Tables[ <<<< 0].Rows)
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
这是我用来执行对存储过程的调用的 powershell 代码。
# Create Shared Helper Objects
$nl = [Environment]::NewLine
function msg
{
param($message,$fgcolor="white",$bgcolor="black")
$output = Write-Host $message -ForegroundColor $fgcolor -BackgroundColor $bgcolor
write-output $output
}
Set-Location "C:\"
$month = Read-Host "Enter Report Month"
$year = Read-Host "Enter Report Year"
msg "$nl Choose a product" "White"
$prompt = ' [D] Desktop [M] Mobile'
switch (Read-Host $prompt) {
"D" {$product = "Desktop"; break}
"M" {$product = "Mobile"; break}
}
msg "$nl Select a Make" "White"
$prompt = ' [F] First [L] Last '
switch (Read-Host $prompt) {
"F" {$SiteMake = "First"; break}
"L" {$SiteMake = "Last"; break}
}
msg "$nl Select a Product" "White"
$prompt = ' [E] Extra [B] Basic'
switch (Read-Host $prompt) {
"E" {$SitePackage = "Extra"; break}
"B" {$SitePackage = "Basic"; break}
}
# Create Database Connection Function
function ConnectSproc
{
param ($SQLServer, $SQLDBName, $SqlQuery)
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
# Report Month
$SqlCmd.Parameters.Add("@ReportMonth",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@ReportMonth"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@ReportMonth"].value = $month
# Report Year
$SqlCmd.Parameters.Add("@ReportYear",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@ReportYear"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@ReportYear"].value = $year
# Product
$SqlCmd.Parameters.Add("@Product",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@Product"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@Product"].value = $product
# Site Make
$SqlCmd.Parameters.Add("@SiteMake",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@SiteMake"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@SiteMake"].value = $SiteMake
# Site Package
$SqlCmd.Parameters.Add("@SitePackage",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@SitePackage"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@SitePackage"].value = $SitePackage
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet) | Out-Null
$SqlConnection.Close()
$DataSet.Tables[0]
}
ConnectSproc "servername" "databasename" "mysproc"
我不知所措。任何帮助,将不胜感激!
更新:实际上看起来程序甚至没有正确执行。这可能会解释我得到的错误。现在我不确定为什么 sproc 没有执行。