此程序是本人学习Winform操作Access数据库时学习笔记。
程序源码来源于:《Visual C# 2008 程序开发入门与提高 》赵增敏 编著
操作方法:
1.新建一个Winform项目,并将该项目设置为启动项目;
2.复制数据库文件到程序根目录下。
3.添加3个Label、两个TextBox、一个ListBox,四个Button控件。
4.窗体底部Label修改名称为“labelMsg”,两个文本框修改名称为:textboxSurname,textboxName。
5.列表框命名为ListBoxEmployee,四个按钮修改名称为:buttonNew、buttonMdodify、buttonDelete、buttonExit.
6.设置窗体、标签、文本框、按钮的Text属性。
程序截图:
源码如下:
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;
namespace ACCESS增删改操作源码
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private OleDbConnection conn;
private OleDbDataAdapter da;
private DataSet ds;
//以下方法用于填充列表框
private void ListBoxFill()
{
if (this.ds.Tables[0].Rows != null) this.ds.Tables[0].Rows.Clear();
this.listBoxEmployee.DataSource = null;
this.listBoxEmployee.Items.Clear();
this.da.Fill(ds, "雇员");
this.listBoxEmployee.DataSource = ds.Tables["雇员"];
this.listBoxEmployee.DisplayMember = "姓名";
this.listBoxEmployee.ValueMember = "雇员ID";
}
//加载窗体时执行以下事件处理程序
private void Form1_Load(object sender, EventArgs e)
{
string NorthwindConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb;";
string quueryString = "select 雇员ID,姓氏,名字,姓氏+名字 AS 姓名 from 雇员";
this.conn = new OleDbConnection(NorthwindConnectionString);
this.ds = new DataSet();
this.ds.Tables.Add("雇员");
this.da = new OleDbDataAdapter(quueryString, conn);
//对数据适配器设置插入命令,insert 语句中包含的问号表示查询参数
this.da.InsertCommand = new OleDbCommand("insert into 雇员(姓氏,名字)values(?,?)", conn);
this.da.InsertCommand.Parameters.AddWithValue("姓氏", "");
this.da.InsertCommand.Parameters.AddWithValue("名字", "");
//对数据适配器设置更新命令,update 语句中包含的问号表示查询参数
this.da.UpdateCommand = new OleDbCommand("update 雇员 set 姓氏=?,名字=? where 雇员ID=?", conn);
this.da.UpdateCommand.Parameters.AddWithValue("姓氏", "");
this.da.UpdateCommand.Parameters.AddWithValue("名字", "");
this.da.UpdateCommand.Parameters.AddWithValue("雇员ID", -1);
//对数据适配器设置删除命令,delete 语句中包含的问号表示查询参数
this.da.DeleteCommand = new OleDbCommand("delete * from 雇员 where 雇员ID=?", conn);
this.da.DeleteCommand.Parameters.AddWithValue("ID", -1);
//用雇员姓名信息填充列表框
this.ListBoxFill();
}
//当单击"新增"按钮时执行以下事件处理程序
private void buttonNew_Click(object sender, EventArgs e)
{
if (this.textBoxSurName.Text == "" || this.textBoxName.Text == "")
{
this.labelMsg.Text = "姓氏和名字不能为空";
return;
}
//通过parameter对象向insert 语句传递参数
this.da.InsertCommand.Parameters["姓氏"].Value = this.textBoxSurName.Text;
this.da.InsertCommand.Parameters["名字"].Value = this.textBoxName.Text;
DataRow dr = this.ds.Tables[0].NewRow(); //在数据表中新增一行
dr["姓氏"] = this.textBoxSurName.Text; //为该行的姓氏列设置值
dr["名字"] = this.textBoxName.Text; //为该行的名字列设置值
this.ds.Tables[0].Rows.Add(dr); //将该行添加到数据表的行集合中。
this.da.Update(this.ds, "雇员"); //将更新保存到数据库表中。
this.ListBoxFill(); //再次填充列表框
this.listBoxEmployee.SelectedIndex = this.listBoxEmployee.Items.Count - 1;
this.labelMsg.Text = "新纪录添加成功";
this.textBoxSurName.Text = "";
this.textBoxName.Text = "";
this.textBoxSurName.Focus();
}
//当单击"更改"按钮时执行一下处理程序
private void buttonModify_Click_1(object sender, EventArgs e)
{
int selectedIndex = this.listBoxEmployee.SelectedIndex;
//通过 parameter 对象向 update 语句传递参数
this.da.UpdateCommand.Parameters["雇员ID"].Value = this.listBoxEmployee.SelectedValue;
this.da.UpdateCommand.Parameters["姓氏"].Value = this.textBoxSurName.Text;
this.da.UpdateCommand.Parameters["名字"].Value = this.textBoxName.Text;
//获取当前选择的数据行
DataRow dr = this.ds.Tables["雇员"].Select("雇员ID=" + this.listBoxEmployee.SelectedValue)[0];
dr["姓氏"] = this.textBoxSurName.Text;
dr["名字"] = this.textBoxName.Text;
this.da.Update(this.ds, "雇员");
this.ListBoxFill();
this.listBoxEmployee.SelectedIndex = selectedIndex;
this.labelMsg.Text = "选定的记录已经被更新";
}
//当单击"删除"按钮时执行以下事件处理程序
private void buttonDelete_Click_1(object sender, EventArgs e)
{
//通过parameter 对象向 delete 语句传递参数
this.da.DeleteCommand.Parameters["ID"].Value = this.listBoxEmployee.SelectedValue;
//获取要删除的数据行
DataRow dr = this.ds.Tables[0].Select("雇员ID=" + this.listBoxEmployee.SelectedValue)[0];
dr.Delete();
this.da.Update(this.ds, "雇员");
this.ListBoxFill();
this.labelMsg.Text = "选定的记录已经被删除";
}
//当在列表框中单击一项时执行以下事件处理程序
private void listBoxEmployee_Click(object sender, EventArgs e)
{
DataRow dr = null;
if (this.listBoxEmployee.SelectedIndex != -1)
{
dr = this.ds.Tables[0].Select("雇员ID=" + this.listBoxEmployee.SelectedValue)[0];
this.textBoxSurName.Text = dr["姓氏"].ToString();
this.textBoxName.Text = dr["名字"].ToString();
}
}
//当单击 退出 按钮时执行以下事件处理程序
private void buttonExit_Click_1(object sender, EventArgs e)
{
Application.Exit();
}
}
}
项目工程源码下载:
链接:http://pan.baidu.com/s/1c0d9HN6 密码:qtgb