C#Winfrom数据库增删改查实例--DataAdapter版
2015-05-25 14:41
302 查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.OleDb; using System.Configuration; namespace TestDbOper2 { public partial class Form1 : Form { static string m_connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; OleDbConnection m_conn; OleDbCommand m_cmd; OleDbDataAdapter m_da; DataTable m_dt; enum DbState { dsAdd,//增加状态 dsMod,//修改状态 dsDel,//删除状态 dsBro //浏览状态 } DbState m_DbState; public Form1() { InitializeComponent(); m_conn = new OleDbConnection(m_connstr); m_cmd = new OleDbCommand(" SELECT * FROM Users", m_conn); m_da = new OleDbDataAdapter(m_cmd); OleDbCommandBuilder cb = new OleDbCommandBuilder(m_da); m_dt = new DataTable(); dataGridView1.DataSource = m_dt; } private void Form1_Load(object sender, EventArgs e) { dataGridView1.AutoGenerateColumns = false; btnQuery_Click(sender, e); } //查询 private void btnQuery_Click(object sender, EventArgs e) { m_DbState = DbState.dsBro; SetBtnState(); m_cmd.CommandText = "select * from users where username like '%"+textBox2.Text+"%'"; m_dt.Clear(); m_da.Fill(m_dt); m_conn.Close(); } //增加 private void btnAdd_Click(object sender, EventArgs e) { m_DbState = DbState.dsAdd; SetBtnState(); txtUserName.Text = ""; txtUserAge.Text = ""; txtUserSex.Text = ""; } //修改 private void btnMod_Click(object sender, EventArgs e) { if (dataGridView1.CurrentCell.RowIndex >= m_dt.Rows.Count) { return; } m_DbState = DbState.dsMod; SetBtnState(); txtUserName.Text = m_dt.Rows[dataGridView1.CurrentCell.RowIndex]["username"].ToString(); txtUserAge.Text = m_dt.Rows[dataGridView1.CurrentCell.RowIndex]["userage"].ToString(); txtUserSex.Text = m_dt.Rows[dataGridView1.CurrentCell.RowIndex]["usersex"].ToString(); } //删除 private void btnDel_Click(object sender, EventArgs e) { if (MessageBox.Show("确定删除吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.No) { return; } try { m_dt.Rows[dataGridView1.CurrentCell.RowIndex].Delete(); m_da.Update(m_dt.GetChanges()); m_dt.AcceptChanges(); MessageBox.Show("删除成功"); } catch(Exception ex) { MessageBox.Show("删除失败"+ex.Message); } } //双击 private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { btnMod_Click(sender, e); } //保存 private void btnSave_Click(object sender, EventArgs e) { if (m_DbState == DbState.dsMod) { try { DataRow dr= m_dt.Rows[dataGridView1.CurrentCell.RowIndex]; dr.BeginEdit(); dr["username"] = txtUserName.Text; dr["userage"] = txtUserAge.Text; dr["usersex"] = txtUserSex.Text; dr.EndEdit(); m_da.Update(m_dt.GetChanges()); m_dt.AcceptChanges(); MessageBox.Show("保存成功"); } catch (Exception ex) { MessageBox.Show("保存失败 " + ex.Message); } } else { try { DataRow dr = m_dt.NewRow(); dr["username"] = txtUserName.Text; dr["userage"] = txtUserAge.Text; dr["usersex"] = txtUserSex.Text; m_dt.Rows.Add(dr); m_da.Update(m_dt.GetChanges()); m_dt.AcceptChanges(); MessageBox.Show("增加成功"); } catch (Exception ex) { MessageBox.Show("增加失败"+ex.Message); } } m_DbState = DbState.dsBro; SetBtnState(); } private void btnCancel_Click(object sender, EventArgs e) { m_DbState = DbState.dsBro; SetBtnState(); } private void SetBtnState() { btnQuery.Enabled = m_DbState == DbState.dsBro; btnAdd.Enabled = m_DbState == DbState.dsBro; btnMod.Enabled = m_DbState == DbState.dsBro; btnDel.Enabled = m_DbState == DbState.dsBro; btnSave.Enabled = m_DbState != DbState.dsBro; btnCancel.Enabled = m_DbState != DbState.dsBro; } } }
相关文章推荐
- C#Winfrom数据库增删改查实例--SQL操作版
- 关于C#winfrom连接数据库的方法及解释
- C# WinFrom 文件绝对路径
- Expression构建DataTable to Entity 映射委托 sqlserver 数据库里面金额类型为什么不建议用float,实例告诉你为什么不能。 sql server 多行数据合并成一列 C# 字符串大写转小写,小写转大写,数字保留,其他除外 从0开始用U盘制作启动盘装Windows10系统(联想R720笔记本)并永久激活方法 纯CSS打造淘宝导航菜单栏 C# Winform
- [转]c# winFrom 使窗体显示SplitContainer或Panel中
- struts+hibernate 请求数据库增删改查(小项目实例)
- C# winfrom下即时显示输出信息 ,比如for循环不等执行完一次输出
- C#Winfrom中,窗体加载时会自动执行一次控件的textchange事件,怎么让它不执行?
- C# Winfrom 默认输入法的设置
- C#通过窗体应用程序操作数据库(增删改查)
- c#存储图片到数据库读数据库显示图片实例
- C# winfrom下绘制圆角窗体
- nodejs_100个实例(4)_mongoose数据库增删改查
- C#操作数据库基础实例《密码管理工具》
- C# WinFrom 对字符进行UTF-8编码
- C#只允许启动一个WinFrom进程的两种方法
- C#连接数据库sqlserver2005,执行存储过程的实例
- C# WinFrom一些技术小结
- ASP.NET 连接数据库 增删改查简单实例
- 在C#中使用控件DataGridView实现数据库增删改查