从MemoryStream创建IMAGE对象时出现的参数无效?
谁可以帮我看看
foreach (DataRow dr in ds.Tables[0].Rows)
{
MemoryStream ms = new MemoryStream();
byte[] img = (byte[])dr[5];
ms.Write(img, 0, img.Length);
MessageBox.Show(img.Length+""+ms.Length);
Image imge = Image.FromStream(ms);//这里说参数ms无效
dr[5] = imge;
ms.Close();
}
出现了参数无效的问题
------解决方案--------------------把你的插入语句修改为如下,再试一试:
String sql = "insert into Addressee values(@add_id,@add_name,@add_number,@add_num,@add_type,@add_image)";
SqlParameter sp_id = new SqlParameter("@add_id", SqlDbType.VarChar);
SqlParameter sp_name = new SqlParameter("@add_name", SqlDbType.VarChar);
SqlParameter sp_number = new SqlParameter("@add_number", SqlDbType.VarChar);
SqlParameter sp_num = new SqlParameter("@add_num", SqlDbType.VarChar);
SqlParameter sp_type = new SqlParameter("@add_type", SqlDbType.VarChar);
SqlParameter sp_image = new SqlParameter("@add_image", SqlDbType.Image);
sp_id.Value = vo.Id;
sp_name.Value = vo.Name;
sp_number.Value = vo.Number;
sp_num.Value = vo.Num;
sp_type.Value = vo.Type;
sp_image.Value = vo.Img;
public int Execute(String sql, List<SqlParamter> Parameters)
{
SqlConnection con = null;
try
{
con = Getcon();
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = sql;
for (int i=0; i<paramArray.Length; i++)
{
cmd.Parameters.Add(paramArray[i]);
}
return cmd.ExecuteNonQuery();
}
finally
{
if(con != null)
con.Close();
}
}
------解决方案--------------------SqlParameter sp_image = new SqlParameter("@add_image", SqlDbType.Image);
SqlDbType.Image类型,不要带长度16,我以前犯过这样的错,16是位。