using System; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApp3 { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } private string index; //新增数据 private void barLargeButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { string sql = @"INSERT INTO Person.ContactType (Name) VALUES (@Name)"; SqlParameter[] sqlParameter = new SqlParameter[1]; sqlParameter[0] = new SqlParameter("@Name", "abbc"); int i = DbHelper.ExecNonQuery(sql, sqlParameter); MessageBox.Show(i.ToString()); } //加载数据 private void Form1_Load(object sender, EventArgs e) { string sql = "SELECT ct.ContactTypeID, ct.Name, ct.ModifiedDate FROM Person.ContactType AS ct "; gridControl1.DataSource = DbHelper.GetDataTable(sql); } //更新数据 private void barLargeButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { string sql = @"UPDATE Person.ContactType SET Name = @Name WHERE ContactTypeID=@ContactTypeID"; SqlParameter[] sqlParameter = new SqlParameter[2]; sqlParameter[0] = new SqlParameter("@Name", "Li Sir"); sqlParameter[1] = new SqlParameter("@ContactTypeID", index); int i = DbHelper.ExecNonQuery(sql, sqlParameter); MessageBox.Show(i.ToString()); } //获取单元格数据 private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) { index = gridView1.GetRowCellDisplayText(e.RowHandle, "ContactTypeID"); } //删除数据 private void barLargeButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { string sql = @"DELETE FROM Person.ContactType WHERE ContactTypeID=@ContactTypeID"; SqlParameter[] sqlParameter = new SqlParameter[1]; sqlParameter[0] = new SqlParameter("@ContactTypeID", index); int i = DbHelper.ExecNonQuery(sql, sqlParameter); MessageBox.Show(i.ToString()); } } }