DataGridView控件对数据库的增、删、改、查实例

      刚学C#不久,做了个记事本提醒器,刚做的时候确实很让人火大,这也不懂那也不懂,还好顶过来了,终于,今天把记事本做完了,觉得有必要对其中用到的几个控件总结一下,一方面可以和大家交流经验,一方面也做个备份,以便以后自己忘记的时候可以看看,好,废话不多说,进入正题:

界面如下:

 

 

上面用的到控件我就不多说了

 

再把代码贴上,由于是个实例,就不做try...catch判断了

 

Form1.cs源码:

 

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.SqlClient;

namespace DataGridView控件增删改查
{
    public partial class Form1 : Form
    {
        DataSet dataSet = new DataSet(); //定义一个数据集
        public Form1()
        {
            InitializeComponent();
        }
       
        //退出功能
        private void CenBtn_Click(object sender, EventArgs e)
        {
            ExitMothed();
        }

        public void ExitMothed()
        {
            DialogResult result;
            result=MessageBox.Show("是否确定要退出!", "退出提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (result == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        //显示全部信息
        public void Display()
        {
            //dataSet.Tables["BuyDetialTemp"].Clear();
            string sql = "select AID,AName,APwd,AJob from Account";    //SQL语句
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, DBHelper.conn);   //定义一个dataAdapter
            dataAdapter.Fill(dataSet, "Account");   //填充数据集
            dataGridView1.DataSource = dataSet.Tables["Account"];  //填充数据进控件

           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Display();
        }

        //删除信息
        private void DelBtn_Click(object sender, EventArgs e)
        {

            string sql = string.Format("delete from Account where AID = '{0}'", dataGridView1.SelectedRows[0].Cells[0].Value);
            SqlCommand com = new SqlCommand(sql, DBHelper.conn);  //定义SqlCommand命令
            DBHelper.conn.Open();  //打开数据库
            com.ExecuteNonQuery();  //更新数据库
            DBHelper.conn.Close();  //关闭数据训
            MessageBox.Show("记录已从数据库删除,请按刷新按钮刷新显示列表", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            dataSet.Tables["Account"].Clear();  //清空控件上的内容
            Display();  //显示数据库内容
        }

        //刷新功能
        private void RefurBtn_Click(object sender, EventArgs e)
        {
            dataSet.Tables["Account"].Clear();  //清空控件上的内容
            Display();  //显示数据库内容
        }
       
        //查询功能
        private void SearchBtn_Click(object sender, EventArgs e)
        {
            string sql = string.Format("select AID,AName,APwd,AJob from Account where AID = '{0}'", TextBIOD.Text.ToString().Trim());
            dataSet.Tables["Account"].Clear(); 
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, DBHelper.conn);
            dataAdapter.Fill(dataSet, "Account");
            dataGridView1.DataSource = dataSet.Tables["Account"];
        }

        //添加功能
        private void AddBtn_Click(object sender, EventArgs e)
        {
            DataTable mytable = dataSet.Tables["Account"];

            string x = textBox1.Text;
            string y = textBox2.Text;
            string z = textBox3.Text;
            string m = textBox4.Text;

            string commandStr = "INSERT INTO Account(AID,AName,APwd,AJob) VALUES ('" + x + "','" + y + "','" + z + "','" + m + "')";     //创建插入语句
            SqlCommand command = new SqlCommand(commandStr, DBHelper.conn);
            DBHelper.conn.Open();
            MessageBox.Show("添加内容成功!");

            command.ExecuteNonQuery();
            DBHelper.conn.Close();

            dataSet.Tables["Account"].Clear();
            Display();

        }

        public void text()
        {
           
        }

        //修改功能
        private void RevBtn_Click(object sender, EventArgs e)
        {
            DataTable mytable = dataSet.Tables["Account"];

            string x = textBox1.Text;
            string y = textBox2.Text;
            string z = textBox3.Text;
            string m = textBox4.Text;

            string commandStr = string.Format("update Account set AID = '{0}',AName = '{1}',APwd = '{2}',AJob = '{3}' where AID = {4}", textBox1.Text.Trim(), textBox2.Text.Trim(), textBox3.Text.Trim(), textBox4.Text.Trim(), dataGridView1.SelectedRows[0].Cells[0].Value);
            SqlCommand command = new SqlCommand(commandStr, DBHelper.conn);
            DBHelper.conn.Open();
            MessageBox.Show("更新成功!");

            command.ExecuteNonQuery();
            DBHelper.conn.Close();

            dataSet.Tables["Account"].Clear();
            Display();
        }
    }
}

 

 

DBHelper.cs源码:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace DataGridView控件增删改查
{
    class DBHelper
    {
        public static string conStr = "Data Source =.;Initial Catalog = AworkeDB;Integrated Security=True";
        public static SqlConnection conn = new SqlConnection(conStr);
    }
}

 

 

这是第一个,明天再写listView控件的

阅读更多
想对作者说点什么? 我来说一句

DataGrid增删改查(EasyUI)示例源码

2016年09月23日 1.64MB 下载

DataGridView 简单的增删改查

2010年01月27日 545KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭