日期:2014-05-17  浏览次数:20341 次

判段某一列值是否为空时出现异常。
程序
 if (sread["head_picture"]==System.DBNull.Value)

出现异常Invalid attempt to read when no data is present.
还有一个问题。
 string sql1 = "insert into users (username,passward,permission,head_picture,mypaper) values ('" + name + "','" + passward + "',0,'@images','" + mypaper + "')";
FileStream fs = File.OpenRead(../../image/1.jpg);
byte[] imageb = new byte[fs.Length];
fs.Read(imageb, 0, imageb.Length);
fs.Close();
SqlCommand com3 = new SqlCommand(sql1, conn);
com3.Parameters.Add("@images", SqlDbType.Image).Value = imageb;
if (com3.Connection.State == ConnectionState.Closed)
      com3.Connection.Open();
try
{
    com3.ExecuteNonQuery();
}
为什么把图片存到数据库里数据不对
sql

------解决方案--------------------

if (string.IsEmptyOrWhiteSpace(sread["head_picture"]))
{

}

------解决方案--------------------
sread是否是DataRow对象? 确认其是否有数据来源.

用法举例

 foreach (DataRow sread in [DataSet对象].Tables[0].Rows)
 {
    if (sread["head_picture"]== System.DBNull.Value)
    {
      ...
    }
}                    

------解决方案--------------------
引用:
还有一个问题。
 string sql1 = "insert into users (username,passward,permission,head_picture,mypaper) values ('" + name + "','" + passward + "',0,'@images','" + mypaper + "')";
FileStream fs = File.OpenRead(../../image/1.jpg);
byte[] imageb = new byte[fs.Length];
fs.Read(imageb, 0, imageb.Length);
fs.Close();
SqlCommand com3 = new SqlCommand(sql1, conn);
com3.Parameters.Add("@images", SqlDbType.Image).Value = imageb;
if (com3.Connection.State == ConnectionState.Closed)
      com3.Connection.Open();
try
{
    com3.ExecuteNonQuery();
}
为什么把图片存到数据库里数据不对

参考 http://hi.baidu.com/keepzeal/item/3faa65106665cd5c2b3e2270