博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Access数据库的增删改查(C#)
阅读量:5173 次
发布时间:2019-06-13

本文共 4074 字,大约阅读时间需要 13 分钟。

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 AddressList

{
    public partial class FrmAccessUse : Form
    {
        public FrmAccessUse()
        {
            InitializeComponent();
        }
        #region 调用access标准方法
        //        using System.Data.OleDb;
//        using System.Data;
//        连接字符串:String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=product.mdb";
//        建立连接:OleDbConnection connection = new OleDbConnection(connectionString);
//        使用OleDbCommand类来执行Sql语句:
//        OleDbCommand cmd = new OleDbCommand(sql, connection);
//        connection.Open();
        //        cmd.ExecuteNonQuery();
        #endregion

        #region 连接字符串

        //string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\程序书籍软件\c#程序代码\access数据库操作\addressList.mdb"; //绝对路径
        string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Environment.CurrentDirectory+"\\addressList.mdb"; //相对路径

        #endregion

        #region 查询

        private void tsbQuery_Click(object sender, EventArgs e)
        {
           query();
        }
        #endregion
        #region 查询语句
        private void query()
        {
                       try
            {
                gridView1.Columns.Clear();
                OleDbConnection connet1 = new OleDbConnection(strcon);
                string sql = "select * from new1";
                connet1.Open();
                OleDbDataAdapter myadapter = new OleDbDataAdapter();
                DataSet ds = new DataSet();
                myadapter.SelectCommand = new OleDbCommand(sql, connet1);
                myadapter.Fill(ds);
                gridControl1.DataSource = ds.Tables[0];
                connet1.Close();
                gridControl1.Refresh();
            }
            catch (Exception exp)
            {

                throw exp;

            }
        }
        #endregion

        #region 保存

        private void tsbSave_Click(object sender, EventArgs e)
        {
            Savebt();
        }
        #endregion
        #region 保存语句
        private void Savebt()
        {
            try
            {
                int[] rowCount = this.gridView1.GetSelectedRows();
                string strSql = "";
                foreach (int i in rowCount)
                {
                    this.gridView1.FocusedRowHandle = -1;
                    string strID = "", strBH = "", strMC = "", strLXID = "", strLX = "", strSM = "", strFZ = "";
                    strID = this.gridView1.GetRowCellValue(i, "ZMID").ToString();
                    strBH = this.gridView1.GetRowCellValue(i, "帐目编号").ToString();
                    strMC = this.gridView1.GetRowCellValue(i, "帐目名称").ToString();
                    strLXID = this.gridView1.GetRowCellValue(i, "帐目类型ID").ToString();
                    strLX = this.gridView1.GetRowCellValue(i, "帐目类型").ToString();
                    strSM = this.gridView1.GetRowCellValue(i, "说明").ToString();
                    strFZ = this.gridView1.GetRowCellValue(i, "附注").ToString();
                    strSql = "Update new1 set 帐目编号='" + strBH + "', 帐目名称='" + strMC + "',帐目类型ID='" + strLXID + "',帐目类型='" + strLX + "',说明='" + strSM + "',附注='" + strFZ + "' where ZMID = " + strID + ""; //注意where处没有单引号               
                }
                OleDbConnection connet1 = new OleDbConnection(strcon);
                connet1.Open();
                OleDbCommand cmd = new OleDbCommand(strSql, connet1);
                int a = cmd.ExecuteNonQuery();
                connet1.Close();
                if (a > 0)
                {
                    MessageBox.Show("保存成功!");
                }
                query();
            }
            catch (Exception exp)
            {

                throw exp;

            }
        }
        #endregion       

        #region 添加一条新记录

        private void tsbAddNew_Click(object sender, EventArgs e)
        {
            addnew();
        }
        #endregion
        #region 添加语句
        private void addnew()
        {
            try
            {
                string strSql = "";
                OleDbConnection connet1 = new OleDbConnection(strcon);
                connet1.Open();
                strSql = "insert into new1(帐目编号) values('NewValues')";
                OleDbCommand cmd = new OleDbCommand(strSql, connet1);
                int a = cmd.ExecuteNonQuery();
                connet1.Close();
                if (a > 0)
                {
                    query();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
         #endregion

        #region 删除

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            delete();
        }
        #endregion
        #region 删除语句
        private void delete()
        {
            try
            {
                int[] rowCount = this.gridView1.GetSelectedRows();
                string strSql = "";
                foreach (int i in rowCount)
                {
                    this.gridView1.FocusedRowHandle = -1;
                    string strID = "";
                    strID = this.gridView1.GetRowCellValue(i, "ZMID").ToString();
                    strSql = "delete from new1 where ZMID=" + strID + ""; //注意where处没有单引号               
                }
                OleDbConnection connet1 = new OleDbConnection(strcon);
                connet1.Open();
                OleDbCommand cmd = new OleDbCommand(strSql, connet1);
                int a = cmd.ExecuteNonQuery();
                connet1.Close();
                if (a > 0)
                {
                    MessageBox.Show("删除成功");
                }
                query();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }

        }
        #endregion
    }
}

 

以上内容引用至

转载于:https://www.cnblogs.com/ruanyifeng/archive/2012/07/26/2609497.html

你可能感兴趣的文章
关于PHP性能优化(转)
查看>>
浙江理工2015.12校赛-F Landlocked
查看>>
一步步学习微软InfoPath2010和SP2010--第四章节--处理SP列表表单(1)--SP列表基础...
查看>>
golang AES/ECB/PKCS5 加密解密 url-safe-base64
查看>>
我是如何用两个星期解决了本来需要两个月而且维护成本巨大的功能(解决思路与方法)...
查看>>
Chapter 2. Overview gradle概览
查看>>
OpenGL的编程环境搭建
查看>>
/etc/fstab 参数详解及如何设置开机自动挂载
查看>>
unity 解决ScrollRect嵌套滚动问题
查看>>
Android Studio安装与配置
查看>>
当前凌晨时间戳
查看>>
mongodb 限制ip访问
查看>>
direction:rtl demo
查看>>
Arch linux LXR 安装过程
查看>>
Games
查看>>
Spring Boot + Spring Cloud 实现权限管理系统 后端篇(五):模块化切分
查看>>
strlen实现
查看>>
js笔记
查看>>
制作具有SSH、MySQL功能的Chroot
查看>>
TWaver html5 + NodeJS + express + websocket.io + redis 快速搭建项目(二)
查看>>