using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.IO;
namespace winform_access
{
public partial class Form1 : Form
{
OleDbConnection conn; //Jet OLEDB:Database Password=
OleDbCommand cmd;
OleDbDataReader dr;
DataTable dt;
string str = null;
public Form1()
{
InitializeComponent();
//数据库的地址
conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\DataSQL\\DB.mdb"); //Jet OLEDB:Database Password=
//conn.Open();
//SaveImage("C:\\Users\\123\\Desktop\\timg.jpg",conn); // 两个 \\ 代表一个 / 字符串 (以下两种也都可以用)
//SaveImage(@"C:\Users\123\Desktop\timg.jpg", conn);
//SaveImage("C:/Users/123/Desktop/timg.jpg", conn);
}
private void button1_Click(object sender, EventArgs e)
{
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "select * from user_DB"; //表名
dr = cmd.ExecuteReader();
dt = new DataTable();
//文件名
if (dr.HasRows)
{
for (int i = 0; i < dr.FieldCount; i++)
{
dt.Columns.Add(dr.GetName(i));
}
dt.Rows.Clear();
}
while (dr.Read())
{
DataRow row = dt.NewRow();
for (int i = 0; i < dr.FieldCount; i++)
{
row[i] = dr[i];
}
dt.Rows.Add(row);
}
cmd.Dispose();
conn.Close();
dataGridView1.DataSource = dt;
}
private void button2_Click(object sender, EventArgs e)
{
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "select * from user_DB where ID=2";
dr = cmd.ExecuteReader();
dt = new DataTable();
if (dr.HasRows)
{
for (int i = 0; i < dr.FieldCount; i++)
{
dt.Columns.Add(dr.GetName(i));
}
dt.Rows.Clear();
}
while (dr.Read())
{
DataRow row = dt.NewRow();
for (int i = 0; i < dr.FieldCount; i++)
{
row[i] = dr[i];
}
dt.Rows.Add(row);
}
cmd.Dispose();
conn.Close();
dataGridView1.DataSource = dt;
}
private void button3_Click(object sender, EventArgs e) //
{
byte[] buff = null;
Image image = null;
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "select img from user_DB where ID=5";
OleDbParameter p = new OleDbParameter();
p.Value = buff;
cmd.Parameters.Add(p);
cmd.ExecuteNonQuery();
dr = cmd.ExecuteReader();
dr.Read();
buff = (byte[])dr[0];
//while (dr.Read())
//{
// buff = (byte[])dr.GetValue(0);
//}
dr.Close();
cmd.Dispose();
conn.Close();
MemoryStream ms = new MemoryStream(buff);
//ms.Position = 0;
image = System.Drawing.Image.FromStream(ms);
//Image image = Image.FromStream(ms);
Bitmap bmpt = new Bitmap(image);
pictureBox1.Image = bmpt;
}
private void SaveImage(string filepath,OleDbConnection con) //将图片以长二进制的形式存入数据库(只有如此才能读取图片
{
con.Open();
FileStream fs = new FileStream(filepath, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] buff = br.ReadBytes((int)fs.Length);
OleDbCommand com = new OleDbCommand("update user_DB set img=@pic where ID=5",con);
com.Parameters.AddWithValue("@pic", buff);
com.ExecuteNonQuery();
con.Close();
com.Dispose();
br.Close();
fs.Close();
}
private void button4_Click(object sender, EventArgs e) //浏览文件
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = "D:\\";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtSelectFile.Text = ofd.FileName; //文件路径会自动转义成 \\双斜杠
}
if (txtSelectFile.Text == "" || txtSelectFile.Text == null)
{
str = null;
}
else
{
str = txtSelectFile.Text;
}
ofd.FileName = null; // 置空,不然点击此处时不选定,取消的话会报错
ofd = null; // 置空,不然点击此处时不选定,取消的话会报错
}
private void button5_Click(object sender, EventArgs e)
{
if (str != null)
{
SaveImage(str, conn);
txtSelectFile.Text = null;
str = null;
}
}
}
}