添加参数以在powershell中执行存储过程

时间:2015-05-12 17:02:36

标签: sql-server powershell

我尝试执行powershell脚本时遇到问题

$UuidMalo=0x0A398A6ADC9BE94CAD6769A81A7A5062
$UuidBueno = 0xA66C97A21D6C2748BD7D7D865205293E

$SQLCmd = New-Object System.Data.SqlClient.SqlCommand
$SQLCmd.CommandType = [System.Data.CommandType]::StoredProcedure
$SQLCmd.CommandText = $SQLCmdString
$SQLCmd.Connection = $SQLConnection


$SQLCmd.Parameters.Add("@contactUUID1",[system.data.SqlDbType]::Binary) 
$SQLCmd.Parameters['@contactUUID1'].Direction = [system.data.ParameterDirection]::Input

$SQLCmd.Parameters['@contactUUID1'].value = $UuidMalo1

执行脚本后的结果是

Must specify a file system
Exception calling "ExecuteNonQuery" with "0" argument(s): "Failed to convert parameter value from a String to a Byte[].
"
At C:\Program Files\ca\PAM\scripts\mergeContacts.ps1:52 char:34
+ $Result = $SQLCmd.ExecuteNonQuery <<<< () | out-null
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

使用convert方法,系统无法将输入转换为输入,因为它很大。

这是SQL

中的数据类型

enter image description here

如何添加参数而不出错...? 感谢

2 个答案:

答案 0 :(得分:0)

我修改了代码并且它有效!但有时连接是一个问题。谢谢!

param(
[String]$UuidMalo, 
[String]$UuidBueno
)


clear; 





$SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
$SQLConnection.ConnectionString = "server=XXXXX-08\XXXXX,1434;database=mdb;user=XXXXX;Password=XXXXXXXX"
$SqlConnection.Open()

$sqlCommand=$sqlConnection.CreateCommand()
$sqlCommand.CommandTimeout = 120
$sqlCommand.CommandText="exec spCheckListContacts " + $UuidMalo+ ","+ $UuidBueno+ ""

$sqlReader = $sqlCommand.ExecuteReader()
$SqlConnection.close()

答案 1 :(得分:0)

param(
[String]$UuidMalo, 
[String]$UuidBueno
)
clear; 
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection "server=XXXXX-08\XXXXX,1434;database=mdb;user=XXXXX;Password=XXXXXXXX"
$SqlConnection.Open()

$sqlCommand=$sqlConnection.CreateCommand()
$sqlCommand.CommandTimeout = 120
$sqlCommand.CommandText="exec spCheckListContacts " + $UuidMalo+ ","+ $UuidBueno+ ""

$sqlReader = $sqlCommand.ExecuteReader()
$SqlConnection.close()


try this i thing in your case it will work