最近给师兄写了个一个程序,在winform 中利用c# 操作access 数据库,完成数据的增删查改。废话不说直接贴code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data.Common;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.Threading;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
//public partial class Form1 : BaseFormAuto
public partial class Form1 : Form
{
private string ConStr;
OleDbConnection con;
string currentdir = System.Environment.CurrentDirectory;
private string ImgPath = System.Environment.CurrentDirectory + "\\tulip.jpg";
private string recordID = "";
int a = 10;
public Form1()
{
InitializeComponent();
//base.GetAllInitInfo(this.Controls[0]);
set_default_values();
//global variable
listView1.View = View.Details;
listView1.Clear();
listView1.HeaderStyle = ColumnHeaderStyle.Clickable;
listView1.FullRowSelect = true;
listView1.GridLines = true;
listView1.Columns.Add("ID", 70, HorizontalAlignment.Center);
listView1.Columns.Add("ST", 60, HorizontalAlignment.Center);
listView1.Columns.Add("INVENTOR", 60, HorizontalAlignment.Center);
listView1.Columns.Add("DATE", 80, HorizontalAlignment.Left);
listView1.Columns.Add("NUAP", 40, HorizontalAlignment.Center);
listView1.Columns.Add("AW1", 60, HorizontalAlignment.Center);
listView1.Columns.Add("AW2", 60, HorizontalAlignment.Center);
listView1.Columns.Add("AW3", 60, HorizontalAlignment.Center);
listView1.Columns.Add("AW4", 60, HorizontalAlignment.Center);
listView1.Columns.Add("AW5", 60, HorizontalAlignment.Center);
listView1.Columns.Add("AW6", 60, HorizontalAlignment.Center);
listView1.Columns.Add("AW7", 60, HorizontalAlignment.Center);
listView1.Columns.Add("AW8", 60, HorizontalAlignment.Center);
listView1.Columns.Add("API2/1", 60, HorizontalAlignment.Center);
listView1.Columns.Add("API3/1", 60, HorizontalAlignment.Center);
listView1.Columns.Add("API4/1", 60, HorizontalAlignment.Center);
listView1.Columns.Add("API5/1", 60, HorizontalAlignment.Center);
listView1.Columns.Add("API6/1", 60, HorizontalAlignment.Center);
listView1.Columns.Add("API7/1", 60, HorizontalAlignment.Center);
listView1.Columns.Add("API8/1", 60, HorizontalAlignment.Center);
listView1.Columns.Add("Other", 60, HorizontalAlignment.Center);
listView1.Columns.Add("img", 100, HorizontalAlignment.Center);
findTime.ShowCheckBox = true;
findTime.Value = DateTime.Now;
listView1.Visible = true;
//init the database when init the form
// init_database();
init_database();
update_list_view();
update_text_rnum();
bool createdNew;
Mutex m = new Mutex(true, "YourAppName", out createdNew);
if (!createdNew)
{
// app is already running…
MessageBox.Show("Only one instance of this application is allowed at a time.");
return;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
//connect date base
private void button1_Click(object sender, EventArgs e)
{
db_open();
if (con.State == ConnectionState.Open)
{
//MessageBox.Show("Access数据库的连接成功!", "Access数据库的连接");
}
else
{
//MessageBox.Show("Access数据库的连接失败!", "Access数据库的连接");
}
db_close();
update_list_view();
set_default_values();
}
//查询
private void button2_Click(object sender, EventArgs e)
{
db_open();
string str = text_id.Text;
//MessageBox(text_aw1.Text);
con.Close();
}
//退出界面
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
//insert a record to database
private void button4_Click(object sender, EventArgs e)
{
db_open();
//String i = textBox2.Text;
check_impty();
StringBuilder sb = new StringBuilder();
sb = get_sql_exp("INSERT");
picBox.Image.Save(System.Environment.CurrentDirectory + "\\temp.png", System.Drawing.Imaging.ImageFormat.Png);
ImgPath = System.Environment.CurrentDirectory + "\\temp.png";
FileStream fs = new FileStream(ImgPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
try
{
//FileStream fs = new FileStream(ImgPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//File.Delete(ImgPath);
byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
//MessageBox.Show(sb.ToString());
string sb1 = sb.ToString();
OleDbCommand cmd = new OleDbCommand(sb1, con);
cmd.Parameters.Add("img", OleDbType.Binary);
cmd.Parameters["img"].Value = imagebytes;
cmd.ExecuteNonQuery();
//fs = null;
//System.GC.Collect();
//MessageBox.Show("One Record Insert Successfully!!!");
//fs.Close();
//File.Delete(ImgPath);
}
catch (Exception ex)
{
MessageBox.Show("Error found when insert date into datebase!!!\nError:\n" + ex.Message);
//fs.Close();
//File.Delete(ImgPath);
}
fs.Close();
File.Delete(ImgPath);
db_close();
update_list_view();
update_text_rnum();
}
//delete all record in UVADB
//private void button5_Click(object sender, EventArgs e)
//{
// var result = MessageBox.Show("Are you sure to delete all records?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
// if (result == DialogResult.OK)
// {
// db_open();
// StringBuilder sql = new StringBuilder();
// sql.Append("delete * from UVADB");
// OleDbCommand cmd = new OleDbCommand(sql.ToString(), con);
// try
// {
// cmd.ExecuteNonQuery();
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.Message);
// }
// db_close();
// update_list_view();
// }
//}
//测试
private void button6_Click(object sender, EventArgs e)
{
}
//ST
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
//ID
private void textBox2_TextChanged(object sender, EventArgs e)
{
string str = text_nuap.Text;
int b = str.IndexOf("-");
bool abc = Convert.ToBoolean(str.IndexOf("-"));
if (str != "" && !(str.IndexOf("-") > 0))
{
checkNumber(str);
try
{
int a = Convert.ToInt16(str.ToString());
if (a < 0 || a > 8)
{
MessageBox.Show("Please input a number beteen 1 and 8!!!");
}
else
{
}
}
catch (Exception re)
{
MessageBox.Show(re.Message);
}
}
}
private void label22_Click(object send