天天看点

如何存取图片到数据库(SQL Server)

以二进制形式通过AppendChunk()方法存到数据库里,再通过Image类的FromStream()方法将图片显示到PictureBox控件中.

AppendChunk()方法:

StringBuilder strSql = new StringBuilder();

                strSql.Append("insert into cpic values(@车牌号,@车图)");

                SqlCommand cmd = new SqlCommand(strSql.ToString(), conn);

                cmd.Parameters.Add("@车牌号", SqlDbType.Text).Value = cph;

                cmd.Parameters.Add("@车图", SqlDbType.Binary).Value = pic;

Image类的FromStream()方法:

                byte[] pic = (byte[])dt.Rows[*][*];

                //MessageBox.Show(dt.Rows[*][*].ToString());

                MemoryStream ms = new MemoryStream(pic);

                this.pictureBox1.Image = Image.FromStream(ms);