【问题标题】:Operand Data Type Clash when executing sp_help_job via VB.Net通过 VB.Net 执行 sp_help_job 时操作数数据类型冲突
【发布时间】:2015-06-01 22:29:09
【问题描述】:

我可以通过以下 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 与 uniqueidentifier 不兼容”。我不确定 int 来自哪里,我的代码没有使用任何 int 数据类型,并且所有参数数据类型都与系统存储过程所期望的匹配。

关于代码的几点说明:我使用的是 OLEDB,因为我的连接字符串作为 OLEDB 连接传递给我。我还连接到 MSDB 数据库。

【问题讨论】:

  • 这可能是因为 JobID 被解析为 int 数据类型,而您在第 4 行将其定义为 uniqueidentifier。尝试将 SqlDbType.UniqueIdentifier 更改为 SqlDbType.Int
  • 根据文档,它应该是一个唯一标识符:msdn.microsoft.com/en-us/library/ms186722.aspx。我尝试将第 4 行设置为 int 数据类型,但这不起作用;我得到同样的错误。
  • 你说得对,应该是uniqueidentifier。我想知道这是否与您如何传递价值有关。我会继续研究。
  • 我删除了 job_id 参数并得到以下信息:“将数据类型 nvarchar 转换为 uniqueidentifier 时出错”。这使得它看起来像是必须使用 job_id,即使我不需要它。也许 vbNull 不等于数据库空值。
  • 看看这个:stackoverflow.com/questions/2490514/… 它是针对 C# 的(我不熟悉 C# 和 VB.NET)。也许您无法将 NULL 发送到 GUID?或者它可能需要不同的格式。像DbNull.Value 这样的东西。所以p1.Value = DbNull.Value

标签: sql-server vb.net tsql


【解决方案1】:

SqlDbType.UniqueIdentifier 引用一个 GUID (see here)。您可能需要重新格式化传递 NULL 值的方式。有关理由,请参阅this link

尝试在第 6 行使用 DbNull.Value,它的内容如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多