前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#使用DataSet类、DataTable类、DataRow类、OleDbConnection类、OleDbDataAdapter类编写简单数据库应用

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

作者头像
CNXY
发布2017-12-25 14:53:25
2.1K0
发布2017-12-25 14:53:25
举报
文章被收录于专栏:C# 编程C# 编程
//注意:请使用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();
        }

    }
}

运行结果:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-11-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档