c#处理SQLSERVER 中image数量类型为空

项目场景:

DataRow dataRow = dataTable.Rows[i];
var pxpicture = dataRow ["pxImage"];
                if (pxpicture!=null)
                {
                    byte[] pic = (byte[])pxpicture;
                    acs.Add("pxpicture", Convert.ToBase64String(pic));
                }


问题描述

代码执行出现错误:

无法将类型为“System.DBNull”的对象强制转换为类型“System.Byte[]”


原因分析:

pxpicture!=null 不能判断pxpicture值是否为空


解决方案:

可以这样写:

if (dataRow["pxImage"] != System.DBNull.Value)
                        {                            
                            byte[] pic = (byte[])pxpicture;
                            uploadAcsInfo.Add("pxpicture", Convert.ToBase64String(pic)); //text    否 破型图照片(以base64传入)
                        }

你可能感兴趣的:(c#,sqlserver)