//存图片
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;
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数据库中读取的方法。通过MemoryStream和OleDb进行数据交互,实现了图片的有效存储及检索。

1682

被折叠的 条评论
为什么被折叠?



