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 = ; i < ; i++)
{
AgeList.Add((i + ).ToString());
}
string [] AgeArray = AgeList.ToArray();
comboBox1.Items.AddRange(AgeArray);
comboBox1.Text = ""; //查找数据库
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=;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<)
{
throw new Exception("身份识别码不能小于6位!");
} //检查是否有身份识别码重复的
for (int i = ; 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 != -)
{
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();
} }
}
运行结果:
C#使用DataSet类、DataTable类、DataRow类、OleDbConnection类、OleDbDataAdapter类编写简单数据库应用的更多相关文章
- DataSet、DataTable、DataRow 复制
DataSet.DataTable.DataRow 复制 DataSet 对象是支持 ADO.NET的断开式.分布式数据方案的核心对象 ,用途非常广泛.我们很多时候需要使用其中的数据,比如取得一个Da ...
- 转:DataSet、DataTable、DataRow、DataColumn区别及使用实例
DataSet 表示数据在内存中的缓存. 属性 Tables 获取包含在 DataSet 中的表的集合. ds.Tables["sjxx"] DataTable 表示内存中数据的 ...
- DataSet、DataTable、DataRow、DataColumn区别及使用实例
DataSet 表示数据在内存中的缓存. 属性 Tables 获取包含在 DataSet 中的表的集合. ds.Tables["sjxx"] DataTable 表示内存中数据的 ...
- ADO.NET中DataSet、DataTable、DataRow的数据复制方法
DataSet 对象是支持 ADO.NET的断开式.分布式数据方案的核心对象 ,用途非常广泛.我们很多时候需要使用其中的数据,比如取得一个DataTable的数据或者复制另一个DataTabe中的数据 ...
- DataSet、DataTable、DataRow的数据复制方法
DataSet 对象是支持 ADO.NET的断开式.分布式数据方案的核心对象 ,用途非常广泛.我们很多时候需要使用其中的数据,比如取得一个DataTable的数据或者复制另一个DataTabe中的数据 ...
- 实际运用中DataSet、DataTable、DataRow点滴
DataSet.DataTable都自带有序列化标记,但是DataRow没有, 所以如果是在CS程序中,Release版本程序DataTable才是最小的数据传输单元,如果使用DataRow则会报[未 ...
- DataSet ,DataTable,DataRow 之间的关系与使用
关系 DataSet 包含多个DataTable,DataTable包含多行DataRow. 使用情况: 有时候GridView等控件需要将数据源动态绑定到DataSet中:将多个DataSe ...
- DataSet、DataTable、DataRow区别
DataSet 表示数据在内存中的缓存. 属性 Tables 获取包含在 DataSet 中的表的集合. ds.Tables["sjxx"] DataTable 表示内存中数据 ...
- DataSet 和 DataTable 以及 DataRow
向DataSet中添加DataTable 会提示datatable已属于另一个dataset 本来的想法是每次都new一个DataTable,但是还是会报错 百度了一下,发现可以调用DataTable ...
随机推荐
- ffmepg命令行参数
ffmpeg使用 有些选项在每个流中都必须指定,例如比特率bitrate或编解码codec.指定流的字符串一般都会有各参数名称和参数,如编解码"-codec:a:1 ac3"表明第 ...
- Day13 SQLAlchemy连表操作和堡垒机
一.数据库操作 1.创建表.插入数据和一对多查询 #!/usr/bin/env python # -*- coding: utf-8 -*- # Author: wanghuafeng from sq ...
- C语言连接MySql数据库
C语言连接MySql数据库(CodeBlocks) 操作系统:Windows7(32位)编译软件:Code::Blocks 10.05数 据 库:mysql-5.1.62-win32 数据库内的数据: ...
- google 论文
从google历年所有论文的汇总来看,TOP5的分别是人工智能和机器学习.算法理论.人机交互与视觉.自然语言处理.机器感知,大家从一个侧面看出goolge research的重点了吧. Google所 ...
- Can deep learning help you find the perfect girl?
Can deep learning help you find the perfect girl? One of the first things I did when I moved to Mont ...
- 宏中"#"和"##"的用法
一.一般用法 我们使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起. 用法: #include<cstdio> #include<climits> using nam ...
- mysql中的group_concat函数的用法
本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) . MySQL中group_concat函数 完整的语法如下: grou ...
- java学习之数组排序一:选择排序
在讲完java中数组定义的两种方式之外,这里需要讲一下对数组常用的方法,第一个就是排序. 加入我们现在有个数组:int[] arr = {12,87,34,3,98,33,103}; 思路1: 1.首 ...
- AlgorithmsI PA2: Randomized Queues and Deques RandomizedQueue
RandomizedQueue 有几个关键点: 1. 选择合适的数据结构,因为需要任意位置删除元素,Linked list 做不到,必须使用resizing arrays. 2. resizing 的 ...
- MPI Maelstrom(Dijkstra)
http://poj.org/problem?id=1502 刷一道模板题稳定一下心情... Dijkstra求单源最短路,就是输入的时候注意下,是按下三角输入的(无向图),输入字符x表示i与j不通. ...