日期:2014-05-18  浏览次数:20867 次

SQL数据库里图片无法读取
我之前把除了图片以外的个人信息全部输进去数据库了,后来又单独输图片(更新上次的记录),图片也已二进制保存进了数据库,可是读取的时侯出现了问题,但是不知道问题出在哪里?以下代码省去了存储其他数据,主要是看看第二部分读取图片
点击picture存储图片
private void pictureBox9_Click(object sender, EventArgs e)
? {
? openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
? if (openFileDialog1.ShowDialog() == DialogResult.OK)
? {
? string fullpath = openFileDialog1.FileName;//文件路径
? FileStream fs = new FileStream(fullpath, FileMode.Open);
? byte[] imagebytes = new byte[fs.Length];//把图片读成字节形式,赋给一个字节数组(image类型是以二进制数字存储)
? BinaryReader br = new BinaryReader(fs);
? imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
? //打开数据库
? SqlConnection cn = new SqlConnection(@"server=.\SQLEXPRESS;AttachDbFilename=C:\USERS\DY\DOCUMENTS\VISUAL STUDIO 2008\PROJECTS\PROPERTY MANAGEMENT SYSTEM\PROPERTY MANAGEMENT SYSTEM\PMS.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True");
? cn.Open();
? SqlCommand cm = new SqlCommand("update [personal info] set ownerPic=@ownerPic where ownerID='"+textBox1.Text+"'", cn);
? cm.Parameters.Add("ownerPic", SqlDbType.Image);
? cm.Parameters["ownerPic"].Value = imagebytes;
? cm.ExecuteNonQuery();
? cn.Close();
? ?
? } ?
? }

读取显示在picture上面

private void button9_Click(object sender, EventArgs e)
? {
? byte[] imagebytes = null;
? //打开数据库
? SqlConnection con = new SqlConnection(@"server=.\SQLEXPRESS;AttachDbFilename=C:\USERS\DY\DOCUMENTS\VISUAL STUDIO 2008\PROJECTS\PROPERTY MANAGEMENT SYSTEM\PROPERTY MANAGEMENT SYSTEM\PMS.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True");
? con.Open();
? SqlCommand com = new SqlCommand("select ownerPic from [personal info] where ownerID='" + textBox1.Text + "'", con);
? SqlDataReader dr = com.ExecuteReader();
? while (dr.Read())
? {
? imagebytes = (byte[])dr.GetValue(1);
? }
? dr.Close();
? com.Clone();
? con.Close();
? MemoryStream ms = new MemoryStream(imagebytes);
? Bitmap bmpt = new Bitmap(ms);
? pictureBox9.Image = bmpt;
? }


------解决方案--------------------
Asp.net是用
Respone.writeBytes

要转成Bitmap 的话还真心没接触过

帮顶吧~~