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(); } } }
运行结果:
作者:CNXY Github:https://www.github.com/cnxy 出处:http://cnxy.me 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出,谢谢! |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?