c# 连接access 增删改一条龙

建立项目 一个datagridview、一个选项卡(3个选项)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string myConnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb";
           OleDbConnection conn = new OleDbConnection(myConnstring);
            OleDbDataAdapter da = new OleDbDataAdapter("select * from t001", conn);
            DataSet ds = new DataSet();
            conn.Open();
            da.Fill(ds);
            conn.Close();
            dataGridView1.DataSource = ds.Tables[0];
 


        }

        private void button1_Click(object sender, EventArgs e)
        {
            string myConnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb";
            OleDbConnection MyConnection;
            MyConnection = new OleDbConnection(myConnstring);
            MyConnection.Open();
            {
                if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" )
                {
                    MessageBox.Show("信息输入不完整,请完整输入!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox1.Focus();
                }
                else
                {
                    string CommandString = "select bukrs ,text ,addr   from t001 where bukrs='" + textBox1.Text.ToString().Trim() + "' and text='" + textBox2.Text.ToString().Trim() + "' and addr='" + textBox3.Text.ToString().Trim() +  "'";
                    OleDbCommand cmd = new OleDbCommand(CommandString, MyConnection);

                    if (cmd.ExecuteScalar() != null)
                    {
                        MessageBox.Show("该记录已存在,请修改输入!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        textBox1.Clear();
                        textBox1.Focus();
                        MyConnection.Close();
                    }
                    else
                    {
                        cmd.CommandText = "insert into [t001]([bukrs],[text],[addr])values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("新数据添加成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        textBox1.Clear();
                        textBox1.Focus();
                        textBox2.Clear();
                        textBox3.Clear();
                      
                    }
                }
            }
           //以下代码就是为了刷新记录集保证datagridview数据更新
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb");
            OleDbDataAdapter da = new OleDbDataAdapter("select * from t001", conn);
            DataSet ds = new DataSet();
            conn.Open();
            da.Fill(ds);
            conn.Close();
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show("您单击的是第" + (e.RowIndex + 1) + "行第" + (e.ColumnIndex + 1) + "列!");
            //MessageBox.Show("单元格的内容是:" + dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
            textBox10.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            textBox11.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            textBox12.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
            textBox6.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            textBox5.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            textBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
            textBox9.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            textBox8.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            textBox7.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            string myConnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb";
            OleDbConnection MyConnection;
            MyConnection = new OleDbConnection(myConnstring);
            MyConnection.Open();

            string CommandString = "delete  from t001 where id= " + Convert.ToInt32(textBox11.Text) + " ";
            OleDbCommand cmd = new OleDbCommand(CommandString, MyConnection);
            cmd.ExecuteNonQuery();
            MyConnection.Close();
            MessageBox.Show("成功删除", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb");
            OleDbDataAdapter da = new OleDbDataAdapter("select * from t001", conn);
            DataSet ds = new DataSet();
            conn.Open();
            da.Fill(ds);
            conn.Close();
            dataGridView1.DataSource = ds.Tables[0];

        }

        private void button3_Click(object sender, EventArgs e)
        {

             string myConnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb";
            OleDbConnection MyConnection;
            MyConnection = new OleDbConnection(myConnstring);
            MyConnection.Open();
            string CommandString = "update [t001] set [bukrs]='" + textBox9.Text + "',[text]='" + textBox8.Text + "',[addr]='" + textBox7.Text + "' where [id]= " + Convert.ToInt32(textBox12.Text) + "";
            OleDbCommand cmd = new OleDbCommand(CommandString, MyConnection);
            cmd.ExecuteNonQuery();
            MyConnection.Close();
            MessageBox.Show("修改成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb");
            OleDbDataAdapter da = new OleDbDataAdapter("select * from t001", conn);
            DataSet ds = new DataSet();
            conn.Open();
            da.Fill(ds);
            conn.Close();
            dataGridView1.DataSource = ds.Tables[0];

            }

         }
   
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT技术与企业应用结合的爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值