从论坛上看到好多人在讨论这方面的问题,把代码整理出来,说不定回答问题的时候还能用上呢。
首先,是数据库的设计。我使用的Access数据库。定义一个数据表photo.mdb,定义两个字段photoID和photo ,其中photoID为主键,字段类型为“文本”,photoID的字段类型为“OLE对象”。(注意:如果采用SQL数据库photo的字段类型为“image “。
以字节形式读写数据库图象字段:
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace WindowsApplication2
... {
public partial class Form1 : Form
...{
public OleDbConnection dbconn = new OleDbConnection();
public OleDbCommand comm = new OleDbCommand();
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
this.OpenConn();
this.pictureBox1.Image = null;
Image im;
this.getPhoto("0000", out im);
this.pictureBox1.Image = im;
}
/**//// <summary>
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace WindowsApplication2
... {
public partial class Form1 : Form
...{
public OleDbConnection dbconn = new OleDbConnection();
public OleDbCommand comm = new OleDbCommand();
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
this.OpenConn();
this.pictureBox1.Image = null;
Image im;
this.getPhoto("0000", out im);
this.pictureBox1.Image = im;
}
/**//// <summary>