利用C#从Access数据库中存取图片

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using System.Data.OleDb; 
namespace WindowsApplication1 { 
	public partial class Form2 : Form { 
		public Form2() { 
			InitializeComponent(); 
		} 
		private void button1_Click(object sender, EventArgs e) { 
			OpenFileDialog ofd = new OpenFileDialog(); 
			if (ofd.ShowDialog() == DialogResult.OK) { 
				label1.Text = ofd.FileName; this.pictureBox1.Image = Image.FromFile(label1.Text); 
			} 
		} 
		OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/student.mdb"); 
		DataSet ds = new DataSet(); 
		private void button2_Click(object sender, EventArgs e) { 
			FileStream fs = new FileStream(label1.Text, FileMode.Open, FileAccess.Read); 
			byte[] data = new byte[fs.Length]; 
			fs.Read(data, 0, data.Length); 
			fs.Close(); 
			OleDbCommand command = new OleDbCommand(); 
			command.CommandText = "insert into pictureTab values(@id,@picture)"; 
			command.Parameters.AddWithValue("@id", textBox1.Text); 
			command.Parameters.AddWithValue("@picture", data); 
			command.Connection = conn; 
			try { 
				conn.Open(); int i = command.ExecuteNonQuery(); 
			} catch (Exception ex) { 
				MessageBox.Show(ex.ToString()); 
			} finally { 
				conn.Close(); 
				command = null; 
			} 
		} 
		private void button3_Click(object sender, EventArgs e) { 
			OleDbCommand command = new OleDbCommand(); command.CommandText = "select picture from pictureTab where (id = @id)"; 
			command.Parameters.AddWithValue("@id", textBox1.Text); 
			command.Connection = conn; 
			OleDbDataAdapter adapter = new OleDbDataAdapter(command); 
			try { 
			//ds = new DataSet(); conn.Open(); adapter.Fill(ds, "picture"); 
			} catch (Exception ex) { 
				MessageBox.Show(ex.ToString()); 
			} finally { 
				conn.Close(); 
				command = null; 
				adapter = null; 
			} 
			byte[] data = (byte[])ds.Tables["picture"].Rows[0]["picture"]; 
			using (MemoryStream memStream = new MemoryStream(data)) { 
				this.pictureBox1.Image = Image.FromStream(memStream); 
				ds.Tables.Clear(); 
			} 
		} 
	} 
} 
/*
int id = int.Parse(textBox1.Text); 
command = new OleDbCommand(); 
command.CommandText = "select picture from ?? where(id=@id)"; 
command.Parameters.AddWithValue("@id", id); 
command.Connection = conn; conn.Open(); 
OleDbDataReader reader = command.ExecuteReader(); 
reader.Read(); 
byte[] data = (byte[])reader["picture"]; 
MemoryStream mem = new MemoryStream(data); 
pictureBox1.Image = Image.FromStream(mem); 
conn.Close();
*/

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木叶流丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值