如何读取 IDataReader 中的空值?

How do I read a null value in the IDataReader?

我正在使用 sqlite 作为数据库。其中一个值在我的数据库中为空。它的数据类型是字节。以下是我用来检索值的代码,

        byte[] blobValue = new byte[iData.GetBytes(4,0,null,0,int.MaxValue)-1];

不幸的是,它抛出“无效转换异常”,因为数据为空。如果数据库中的数据为空,我如何return null?

您可以使用 IsDBNull:

byte[] blobValue = null;
if(!iData.IsDBNull(4))
{
    blobValue = new byte[iData.GetBytes(4,0,null,0,int.MaxValue)-1];
}