Powershell and SQL parameters. If empty string, pass DBNull

后端 未结 4 467

I got this parameter:

$objDbCmd.Parameters.Add(\"@telephone\", [System.Data.SqlDbType]::VarChar, 18) | Out-Null;
$objDbCmd.Parameters[\"@telephone\"].Value =         


        
4条回答
  •  滥情空心
    2021-01-18 07:57

    I don't know about powershell, but in C# I would do something like this:

    if ([string]::IsNullOrEmpty($objUser.Telephone))
    {
     $objDbCmd.Parameters["@telephone"].Value = [DBNull]::Value;
    }
    else
    {
     $objDbCmd.Parameters["@telephone"].Value = $objUser.Telephone;
    }
    

提交回复
热议问题