c# 操作Access 存取图片(转)

//存图片
MemoryStream stream = new MemoryStream();
byte[] photo = null;
Image img = this.pictureBox1.Image;
img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
photo = stream.ToArray();
stream.Close();
string sql = "Insert into Tb (ID,Photo) Values ('1',@img)";
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\d.mdb");
conn.Open();
OleDbCommand comm = new OleDbCommand(sql, conn);
comm.Parameters.Add("@img", OleDbType.VarBinary, photo.Length).Value = photo;
comm.ExecuteNonQuery();
conn.Close();


//取图片
string sql = "Select photo From Tb Where ID='1'";
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\A.mdb");
conn.Open();
OleDbCommand comm = new OleDbCommand(sql, conn);
OleDbDataReader sdr = comm.ExecuteReader();
sdr.Read();
MemoryStream ms = new MemoryStream((byte[])sdr[0]);
Image image = Image.FromStream(ms);
sdr.Close();
conn.Close();
pictureBox1.Image = image;

你可能感兴趣的:(C#,ACCESS)