2008-11-18 215 views
0

我有一个Web服务,它有一个通用函数,可以从存储过程的结果中返回数据集...某些存储过程具有可选参数,其值可以为空,但不是全部。DbNull.Value存储过程参数?

反正我想在其中具有DBNull.Value

的值的参数来传递,我得到这个时出错生成XML文档。当这样做的时候从web服务返回

如果我离开这个参数它工作正常......但真的想知道为什么DBNull.Value导致此问题。

回答

1

我认为这是因为System.DBNull值是数据库表中的空值,但过程中的空字段实际上等同于null/nothing关键字。不是数据库空值。我不确定技术上的差异。

但是在你的存储过程中,你可以将它默认为null,而不是像你已经完成的那样发送值,或者我相信如果你发送了null/nothing,它也可以工作。

0

您可以在SqlParemeter中传递NULL值,但您必须进行一些类型转换以确保传递正确的空值。

在这个例子中,有一个被称为“计数”的参数是一个整数,它被作为空传递:

Using dtResult as New DataTable 
    Using cn as SqlConnection = New SqlConnection ("ConnectionString") 
    Using sqlcmd as SqlCommand - New SqlCommand("StoredProceName", cn) with {.CommandType=CommandType.StoredProcedure} 

     Dim sp As SqlParameter 
     sp = sqlcmd.Parameters.Add("@Count", SqlDbType.Int) 
     sp.value = CType(Nothing, SqlTypes.SqlInt32) 

     Using myDR as SqlDataReader = sqlcmd.ExecuteReader 
     dtResult.Load(myDR) 
     end using 
     return dtResult 
    End Using ' For sqlcmd 
    cn.Close() ' Close the connection 
    end using ' For cn 
end using ' For dtResult