我昨夜研究才得,我发给你.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;
namespace 插入图片完整版
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
string fullname;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
OleDbConnection con;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
private System.Windows.Forms.TextBox picName;
OleDbDataAdapter da;
DataSet ds;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.picName = new System.Windows.Forms.TextBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.White;
this.pictureBox1.Location = new System.Drawing.Point(40, 80);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(416, 352);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(528, 15);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 23);
this.button1.TabIndex = 1;
this.button1.Text = "浏览图片";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(616, 15);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 2;
this.button2.Text = "保 存";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// picName
//
this.picName.Location = new System.Drawing.Point(256, 15);
this.picName.Name = "picName";
this.picName.Size = new System.Drawing.Size(248, 21);
this.picName.TabIndex = 3;
this.picName.Text = "";
//
// dataGrid1
//
this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.ActiveCaptionText;
this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(504, 88);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(176, 344);
this.dataGrid1.TabIndex = 4;
this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
this.dataGridTableStyle1});
this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
//
// dataGridTableStyle1
//
this.dataGridTableStyle1.DataGrid = this.dataGrid1;
this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
this.dataGridTextBoxColumn1});
this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGridTableStyle1.MappingName = "image1";
//
// dataGridTextBoxColumn1
//
this.dataGridTextBoxColumn1.Format = "";
this.dataGridTextBoxColumn1.FormatInfo = null;
this.dataGridTextBoxColumn1.HeaderText = "图片编号:";
this.dataGridTextBoxColumn1.MappingName = "id";
this.dataGridTextBoxColumn1.Width = 75;
//
// label1
//
this.label1.Location = new System.Drawing.Point(48, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(296, 23);
this.label1.TabIndex = 5;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(696, 454);
this.Controls.Add(this.label1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.picName);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
this.openFileDialog1.Filter = "图片文件(*.jpg;*.gif;*.bmp)|*.jpg;*.gif;*.bmp";
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
fullname=this.openFileDialog1.FileName;
this.pictureBox1.Image=Bitmap.FromFile(fullname);
this.picName.Text=fullname;
int i=this.openFileDialog1.FileName.LastIndexOf("\\")+1;
this.fullname=fullname.Substring(i);
}
}
catch(Exception er)
{
MessageBox.Show(er.Message);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
try
{
FileStream fs=File.OpenRead(fullname);
byte [] content=new byte[fs.Length];
fs.Read(content,0,content.Length);
fs.Close();
OleDbCommand com=new OleDbCommand("insert into image1(pic,picname)values(@pic,'"+this.fullname.Trim().ToString()+"')",con);
com.Parameters.Add("@pic",OleDbType.Binary);
com.Parameters["@pic"].Value=content;
com.ExecuteNonQuery();
// con.Close();
MessageBox.Show("图片成功保存!");
this.load_data();
}
catch(Exception er)
{
MessageBox.Show(er.Message);
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.load_data();
}
void load_data()
{
con=new OleDbConnection(@"provider=microsoft.jet.oledb.4.0;data source="+AppDomain.CurrentDomain.BaseDirectory+"image.mdb");
con.Open();
da=new OleDbDataAdapter("select * from image1",con);
ds=new DataSet();
da.Fill(ds);
this.dataGrid1.DataSource=ds.Tables[0].DefaultView;
}
private int Cur()
{
return this.dataGrid1.CurrentRowIndex;
}
private void dataGrid1_Click(object sender, System.EventArgs e)
{
try
{
//int cur=this.dataGrid1.CurrentRowIndex;
int cur=this.Cur();
this.label1.Text=this.ds.Tables[0].Rows[cur]["picname"].ToString();
MemoryStream ms=new MemoryStream((byte[])this.ds.Tables[0].Rows[cur]["pic"]);
Image image=Image.FromStream(ms,true);
this.pictureBox1.Image=image;
}
catch(Exception er)
{
MessageBox.Show(er.Message);
}
}
}
}