Winform操作Access数据库增删改操作学习笔记

此程序是本人学习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

时间: 2024-08-13 14:41:38

Winform操作Access数据库增删改操作学习笔记的相关文章

MVC操作SQL数据库增删改查

控制器代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MvcApplication1.Models; namespace MvcApplication1.Controllers { public class HomeController : Controller { // // GET: /Home/ publi

C#学习笔记(3)——操作sqlserver数据库增删改查

说明(2017-5-25 16:29:35): 1. VS2010,视图->服务器资源管理器->数据连接->右键添加连接->服务器名(本机可以用点)->选择数据库->高级里面可以看其他选项,及连接字符串

C# winform窗体设计-对数据库执行增删改操作

对于学习数据库的人来说,数据库的增删改可谓是最基本的了(小编其实也只是一个小白=-=),这篇文章,小编将于大家讲解数据库增删改操作 在执行数据库增删改的时候主要使用的:Command 类       ExecuteNonQuery执行命令,此命令带有返回值(int) 下面介绍对数据库执行操作的具体步骤: (1)创建连接 (2)创建命令 (3)设置命令的连接和文本(sql语句) (4)打开连接 (5)执行命令 (6)使用命令返回的数据 (7)关闭连接 代码演示: 1 string s = "ser

(转)SQLite数据库增删改查操作

原文:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html SQLite数据库增删改查操作 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字).TEXT(字符串文本)和BLOB(二进制对象)数据类型,虽然它支持的类型只有五种,但实际上sqlite3也接受varchar(n).char(n).d

Yii2.0高级框架数据库增删改查的一些操作

yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 ----------------------------------------------------------------------------------------------------- User::find()->all();    //返回所有用户数据:User::findOne($id);   //返回 主键 id=1  的一条数

Java+MyEclipse+Tomcat (六)详解Servlet和DAO数据库增删改查操作

此篇文章主要讲述DAO.Java Bean和Servlet实现操作数据库,把链接数据库.数据库操作.前端界面显示分模块化实现.其中包括数据的CRUD增删改查操作,并通过一个常用的JSP网站前端模板界面进行描述.参考前文: Java+MyEclipse+Tomcat (一)配置过程及jsp网站开发入门 Java+MyEclipse+Tomcat (二)配置Servlet及简单实现表单提交 Java+MyEclipse+Tomcat (三)配置MySQL及查询数据显示在JSP网页中 Java+MyE

easyui学习笔记1—增删改操作【转载】

最近公司要用easyui,这里自己看了官网几篇文章,遇到些问题,大多数的问题都是敲代码的时候笔误,其他有些地方确实需要注意一下,这里做些笔记. 1.在mysql中建好表之后修改id字段为递增字段,发现这个奇怪的mysql语法,如下 alter table student change id id int auto_increment; 这句是在student表已经建好的情况下来修改字段id为自增列,奇怪的是为嘛change id id,并且后面还要带上id的类型int? 2.html5标记 如何

【2017-05-02】winform弹出警告框是否进行增删改操作、记事本制作、对话框控件和输出输入流

一.winform弹出警告框是否进行增删改操作 第一个参数是弹出窗体显示的内容,第二个参数是标题,第三个参数是该弹窗包含确定和取消按钮. 返回的是一个枚举类接收一下. 再进行判断,如果点的是确定按钮,再进行下一步的增删改操作. 二.记事本的制作 1.菜单工具栏MenuStrip-插入标准项 2.TextBox -显示部分 小箭头 MultiLine 选中多行 Dock属性占满. 3.功能 - 撤销 - 剪切 - 粘贴 - 复制 - 删除 - 全选 - 时间 - 查找 单独做一个窗体点击打开 把主

Yii2.0高级框架数据库增删改查的一些操作(转)

yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 ----------------------------------------------------------------------------------------------------- User::find()->all();    //返回所有用户数据:User::findOne($id);   //返回 主键 id=1  的一条数