C#使用DataSet类、DataTable类、DataRow类、OleDbConnection类、OleDbDataAdapter类编写简单数据库应用...

//注意:请使用VS2010打开以下的源代码。
//源代码地址:http://pan.baidu.com/s/1j9WVR
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        OleDbConnection connection;
        OleDbDataAdapter command;
        DataSet dataSet;
        DataTable table;

        OleDbCommandBuilder builder;

        private void Form1_Load(object sender, EventArgs e)
        {
            //增加年龄数据(1~100)
            List<string> AgeList = new List<string>();
            for (int i = 0; i < 100; i++)
            {
                AgeList.Add((i + 1).ToString());
            }
            string [] AgeArray = AgeList.ToArray();
            comboBox1.Items.AddRange(AgeArray);
            comboBox1.Text = "20";

            //查找数据库
            connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Info.mdb;");
            command = new OleDbDataAdapter("Select * From Information", connection);
            dataSet = new DataSet("Info");
            command.Fill(dataSet, "Information");

            builder = new OleDbCommandBuilder(command);

            //显示数据库
            table = dataSet.Tables["Information"];
            dataGridView1.DataSource = table;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            char[] tempChars = textBox1.Text.Trim().ToArray();
            List<char> validChars= new List<char>();

            for (int i=0;i<tempChars.Length;i++)
            {
                if(!char.IsNumber(tempChars[i]))
                {
                    tempChars=validChars.ToArray();
                    textBox1.Text = new string(tempChars);
                    textBox1.SelectionStart = textBox1.Text.Length;
                    break;
                }
                else
                {
                    validChars.Add(tempChars[i]);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text.Trim() == "")
                {
                    throw new Exception("身份识别码不能空!");
                }
                else if(textBox1.Text.Trim().Length<6)
                {
                    throw new Exception("身份识别码不能小于6位!");
                }

                //检查是否有身份识别码重复的
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    if ((string)table.Rows[i]["ID"] == textBox1.Text.Trim())
                    {
                        throw new Exception("已经存在" + textBox1.Text.Trim() + ",请勿重复添加!");
                    }
                }

                //添加操作
                DataRow row = table.NewRow();
                row["ID"] = textBox1.Text.Trim();
                row["Name"] = textBox2.Text.Trim();
                row["Age"] = comboBox1.Text;
                if (radioButton1.Checked == true)
                {
                    row["Gender"] = "";
                }
                else
                {
                    row["Gender"] = "";
                }
                table.Rows.Add(row);


                command.Update(dataSet, "Information");
                dataGridView1.DataSource = table;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                //删除操作
                if (dataGridView1.CurrentCell == null)
                {
                    throw new Exception("无任何内容可删!");
                }

                if (dataGridView1.CurrentCell.RowIndex != -1)
                {
                    table.Rows[dataGridView1.CurrentCell.RowIndex].Delete();
                }
                else
                {
                    throw new Exception("未在表格内选择任一个单元格!");
                }

                command.Update(dataSet, "Information");
                dataGridView1.DataSource = table;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //表格上的内容填至相应的文本框等控件
            textBox1.Text =(string) dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["ID"].Value;
            textBox2.Text = (string)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["Name"].Value;
            comboBox1.Text=(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["Age"].Value).ToString();
            if ((string)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["Gender"].Value == "")
            {
                radioButton1.Checked = true;
            }
            else
            {
                radioButton2.Checked = true;
            }
        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            textBox1.SelectAll();
        }

        private void textBox2_MouseClick(object sender, MouseEventArgs e)
        {
            textBox2.SelectAll();
        }

    }
}

运行结果:

转载于:https://www.cnblogs.com/cncc/p/3440620.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值