sql server语言 - 通过VB.Net执行sp_help_job时操作数数据类型冲突



我能够通过以下TSQL轻松获取sql server代理作业信息:

sp_help_job @job_id=null, @job_name='<My Job Name>',@job_aspect='JOB'

然而,当我尝试在vb.net中执行这个系统存储过程时,我得到一个数据类型冲突错误:

cmd = New OleDb.OleDbCommand("sp_help_job", jobConnection)
cmd.CommandType = CommandType.StoredProcedure
''job id value should be set to null - we will filter on the job name...
p1 = New OleDb.OleDbParameter("@Job_id", SqlDbType.UniqueIdentifier)
p1.Direction = ParameterDirection.Input
p1.Value = vbNull
cmd.Parameters.Add(p1)
''job name paramter is the currently running interface, strCurrentPackage...
p2 = New OleDb.OleDbParameter("@job_name", SqlDbType.NVarChar)
p2.Direction = ParameterDirection.Input
p2.Value = strCurrentPackage
cmd.Parameters.Add(p2)
''job aspect value = "JOB" to get only the job info result set...
p3 = New OleDb.OleDbParameter("@job_aspect", SqlDbType.NVarChar)
p3.Direction = ParameterDirection.Input
p3.Value = "JOB"
cmd.Parameters.Add(p3)
jobReader = cmd.ExecuteReader()

错误是"操作数类型冲突:int与唯一标识符不兼容"。我不确定int是从哪里来的,我的代码没有使用任何int数据类型,所有参数数据类型都匹配系统存储过程所期望的。

关于代码的几个注意事项:我使用OLEDB是因为我的连接字符串作为OLEDB连接传递给我。我也连接到MSDB数据库。

SqlDbType.UniqueIdentifier引用GUID(见这里)。您可能需要重新格式化传递NULL值的方式。

尝试在第6行使用DbNull.Value,因此它是这样读的:

p1 = New OleDb.OleDbParameter("@Job_id", SqlDbType.Int)
p1.Direction = ParameterDirection.Input
p1.Value = DbNull.Value
cmd.Parameters.Add

最新更新