使用powershell插入到oracle表

时间:2016-10-24 06:09:30

标签: oracle powershell

我想使用PowerShell编写一个oracle表。我写了以下代码。

    [System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient")
$connectionString = "User Id=XXX;Password=XXX;Data Source=YYY;"
$connection = New-Object System.Data.OracleClient.OracleConnection($connectionString)

$app_id="SP_ERROR_ALERT"
$cat="CAT151"
$stat="I"
$phoneno="0123456789"
$body="Hard drive free space running low - SP"
$outid="SLIC%20LIFE"
$desc="Free space"

$connection.Open()

$queryString = "insert into DB.TABLE (APPLICATION_ID, JOB_CATEGORY, SMS_TYPE, MOBILE_NUMBER, TEXT_MESSAGE, SHORT_CODE, JOB_OTHER_INFO) VALUES (:one, :two, :three, :four, :five, :six, :seven) "

$command.Parameters.Add("one", [System.Data.OracleClient.OracleType]::VarChar)
$command.Parameters["one"].Value = $app_id

$command.Parameters.Add("two", [System.Data.OracleClient.OracleType]::VarChar)
$command.Parameters["two"].Value = $cat

$command.Parameters.Add("three", [System.Data.OracleClient.OracleType]::VarChar)
$command.Parameters["three"].Value = $stat

$command.Parameters.Add("four", [System.Data.OracleClient.OracleType]::VarChar)
$command.Parameters["four"].Value = $phoneno

$command.Parameters.Add("five", [System.Data.OracleClient.OracleType]::VarChar)
$command.Parameters["five"].Value = $body

$command.Parameters.Add("six", [System.Data.OracleClient.OracleType]::VarChar)
$command.Parameters["six"].Value = $outid

$command.Parameters.Add("seven", [System.Data.OracleClient.OracleType]::VarChar)
$command.Parameters["seven"].Value = $desc

$command = New-Object System.Data.OracleClient.OracleCommand
$command.Connection = $connection
$command.CommandType = [System.Data.CommandType]::Text
$command.CommandText = $queryString
$command.ExecuteNonQuery()

$connection.Close()

当我执行脚本时,我得到一个错误,说“异常调用”ExecuteNonQuery“with”0“参数:”ORA-01008:并非所有变量都绑定了。“我在这里做错了什么。

提前致谢

2 个答案:

答案 0 :(得分:2)

你可以像这样减少你的代码:

   [System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient")

   $connectionString = "User Id=XXX;Password=XXX;Data Source=YYY;"
   $connection = $null
   $command = $null

   Try
   {
       $queryString = "insert into SMS.SMS_GATEWAY (APPLICATION_ID, JOB_CATEGORY, SMS_TYPE, MOBILE_NUMBER, TEXT_MESSAGE, SHORT_CODE, JOB_OTHER_INFO) VALUES (:one, :two, :three, :four, :five, :six, :seven) "

       $connection = New-Object System.Data.OracleClient.OracleConnection($connectionString)
       $command = New-Object System.Data.OracleClient.OracleCommand -ArgumentList $queryString, $connection  

       $connection.Open()

       $command.Parameters.Add("one", "SP_ERROR_ALERT")
       $command.Parameters.Add("two", "CAT151")
       $command.Parameters.Add("three", "I")
       $command.Parameters.Add("four", "012345679")
       $command.Parameters.Add("five", "Hard drive free space running low - SP")
       $command.Parameters.Add("six", "SLIC%20LIFE")
       $command.Parameters.Add("seven", "Free space")

       $command.ExecuteNonQuery()
   }
   Finally
   {
       if ($connection -ne $null) 
       {
          $connection.Close()
          $connection.Dispose()
       }

       if ($command -ne $null) 
       {
          $command.Dispose()
       }

   }

答案 1 :(得分:0)

在评论的帮助下,我能够解决我遇到的问题。

这是我的工作代码。

    [System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient")

$connectionString = "User Id=XXX;Password=XXX;Data Source=YYY;"
$connection = New-Object System.Data.OracleClient.OracleConnection($connectionString)

Try
{
$queryString = "insert into SMS.SMS_GATEWAY (APPLICATION_ID, JOB_CATEGORY, SMS_TYPE, MOBILE_NUMBER, TEXT_MESSAGE, SHORT_CODE, JOB_OTHER_INFO) VALUES (:one, :two, :three, :four, :five, :six, :seven) "
    $command = New-Object System.Data.OracleClient.OracleCommand
    $command.Connection = $connection
    $command.CommandType = [System.Data.CommandType]::Text
    $command.CommandText = $queryString



    $app_id="SP_ERROR_ALERT"
    $cat="CAT151"
    $stat="I"
    $phoneno="012345679"
    $body="Hard drive free space running low - SP"
    $outid="SLIC%20LIFE"
    $desc="Free space"

    $connection.Open()


    $pone=New-Object System.Data.OracleClient.OracleParameter
    $pone.ParameterName = "one"
    $pone.Value = $app_id

    $ptwo=New-Object System.Data.OracleClient.OracleParameter
    $ptwo.ParameterName = "two"
    $ptwo.Value = $cat

    $pthree=New-Object System.Data.OracleClient.OracleParameter
    $pthree.ParameterName = "three"
    $pthree.Value = $stat

    $pfour=New-Object System.Data.OracleClient.OracleParameter
    $pfour.ParameterName = "four"
    $pfour.Value = $phoneno

    $pfive=New-Object System.Data.OracleClient.OracleParameter
    $pfive.ParameterName = "five"
    $pfive.Value = $body

    $psix=New-Object System.Data.OracleClient.OracleParameter
    $psix.ParameterName = "six"
    $psix.Value = $outid

    $pseven=New-Object System.Data.OracleClient.OracleParameter
    $pseven.ParameterName = "seven"
    $pseven.Value = $desc

    $command.Parameters.Add($pone)
    $command.Parameters.Add($ptwo)
    $command.Parameters.Add($pthree)
    $command.Parameters.Add($pfour)
    $command.Parameters.Add($pfive)
    $command.Parameters.Add($psix)
    $command.Parameters.Add($pseven)

    $command.ExecuteNonQuery()
}
Catch 
{

}
Finally
{
    $connection.Close()
}