如何在C#中比较Blob字段的空值 [英] how to compare the null value of a blob field in C#
本文介绍了如何在C#中比较Blob字段的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我在C#中使用mysql.我将图像文件存储到具有BLOB数据类型的mysql中.
但是,每当我从该字段中检索值时,我都想检查它是否为NULL.我该怎么办?
类似于下面的代码.它不起作用.
Hello,
I''m using mysql with c#. I''m storing image files into mysql with BLOB data-type.
But whenever I''m retrieving the value from this field, I want to check whether it''s NULL or not. How can I do that?
Something like below code. It''s not working.
if ((ds3.Tables["tempEmpDetails"].Rows[0]["Emp_Photo"] != System.DBNull))
{
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(ds3.Tables["tempEmpDetails"].Rows[0]["Emp_Photo"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox2.Image = Image.FromStream(stmBLOBData);
}
else
{
pictureBox2.Image = Properties.Resources.profile;
}
推荐答案
您无法针对System.DBNull进行测试-这是一个类.请使用System.DBNull.Value,或尝试以下操作:
You can''t test against System.DBNull - that''s a class. Either use System.DBNull.Value, or try this:
object o = ds3.Tables["tempEmpDetails"].Rows[0]["Emp_Photo"];
if (o is System.DBNull))
{
pictureBox2.Image = Properties.Resources.profile;
}
else
{
Byte[] byteBLOBData = (Byte[])(o);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox2.Image = Image.FromStream(stmBLOBData);
}
这篇关于如何在C#中比较Blob字段的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文