DBHELPER.CS查、增、删、改例子
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.SqlClient; using DevExpress.XtraEditors; namespace DXApplication5 { public partial class Form1 : DevExpress.XtraEditors.XtraForm { public Form1() { InitializeComponent(); } //绑定数据表 private void simpleButton1_Click(object sender, EventArgs e) { //返回封装的表形式 //this.gridControl1.DataSource = DBHelper.GetDataTable("select * from myuser"); //普通表 this.gridControl1.DataSource = DBHelper.GetDataSet("select * from myuser").Tables[0]; } //清空数据表 private void simpleButton5_Click(object sender, EventArgs e) { this.gridControl1.DataSource = null; } /// <summary> /// 表示每行生成的时候随绘制这一行数据的时候触发 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { if (e.Info.IsRowIndicator && e.RowHandle >= 0)//在表格前面加行号 { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } } /// <summary> /// 表示获取某行中某列的数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void gridView1_Click(object sender, EventArgs e) { //当表不为空时获取单元格数据有效 if (this.gridControl1.DataSource != null) { //获取点击的行数 int selectedHandle = this.gridView1.GetSelectedRows()[0]; //得到该行中某列绑定字段的值 this.textEdit1.Text = this.gridView1.GetRowCellValue(selectedHandle, "username").ToString().Trim(); this.textEdit2.Text = this.gridView1.GetRowCellValue(selectedHandle, "mygroup").ToString().Trim(); this.textEdit3.Text = this.gridView1.GetRowCellValue(selectedHandle, "mystop").ToString().Trim(); } } //新建一条数据 private void simpleButton2_Click(object sender, EventArgs e) { //SQL语句:先查询数据是否已经存在相同的数据 string SSQL = "select * from myuser where username='" + Convert.ToString(textEdit1.Text.Trim()) + "'"; //使用ExecuteNonQuery指令SELECT查询时不会返回行数,因此查询需要使用ExecuteScalar指令 if (DBHelper.ExecuteScalar(SSQL) == null) { string ADDSQL = "insert into myuser(username,mygroup,mystop) values('" + Convert.ToString(textEdit1.Text.Trim()) + "','" + Convert.ToString(textEdit2.Text.Trim()) + "','" + Convert.ToString(textEdit3.Text.Trim()) + "')"; int m = DBHelper.ExecuteNonQuery(ADDSQL); if (m > 0) { //刷新数据表 this.gridControl1.DataSource = DBHelper.GetDataTable("select * from myuser"); } } else { XtraMessageBox.Show("添加失败,已存在相同数据"); } } //删除一条数据 private void simpleButton3_Click(object sender, EventArgs e) { //SQL语句 string SQL = "delete from myuser where username='" + Convert.ToString(textEdit1.Text.Trim()) + "'"; int n = DBHelper.ExecuteNonQuery(SQL); if (n > 0) { //刷新数据表 this.gridControl1.DataSource = DBHelper.GetDataTable("select * from myuser"); } else { XtraMessageBox.Show("删除失败"); } } //修改一条数据 private void simpleButton4_Click(object sender, EventArgs e) { //SQL语句 string SQL = "Update myuser set mygroup='" + textEdit2.Text.Trim() + "',mystop='" + textEdit3.Text.Trim() + "' " + " where username=" + "'" + textEdit1.Text.Trim() + "'"; int n = DBHelper.ExecuteNonQuery(SQL); if (n > 0) { //刷新数据表 this.gridControl1.DataSource = DBHelper.GetDataTable("select * from myuser"); } else { XtraMessageBox.Show("修改失败"); } } } }