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.con nectionstrings["ConnStr"]. ConnectionString; OleDbConnection M_conn; OleDbCommand M_cmd; OleDbDataAdapter M_da; DataTable M_dt; Enum Dbstate {dsadd,//Add state dsmod,//Modify State dsdel,//Delete State Dsbro//Browse State } 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); }//query private void Btnquery_click (object sender, EventArgs e) {m_dbstate = Dbstate.dsbro ; Setbtnstate (); M_cmd.commandtext = "SELECT * from the users where username like '%" +textbox2.text+ "% '"; M_dt. Clear (); M_da. Fill (M_DT); M_conn. Close (); }//Added private void btnAdd_Click (object sender, EventArgs e) {m_dbstate = Dbstate.dsadd; Setbtnstate (); txtUsername.Text = ""; Txtuserage.text = ""; Txtusersex.text = ""; }//Modify private void Btnmod_click (object sender, EventArgs e) {if (datagridview1.currentce ll. 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 (); }//delete private void Btndel_click (object sender, EventArgs e) {if (MessageBox.Show ("OK" to delete? "," hint ", messageboxbuttons.yesno) = = dialogresult.no) {return;} try {M_dt. Rows[datagridview1.currentcell.rowindex]. Delete (); M_da. Update (M_dt. GetChanges ()); M_dt. AcceptChanges (); MessageBox.Show ("delete succeeded"); } catch (Exception ex) {MessageBox.Show ("Delete failed" +ex. Message); }}//double-click private void Datagridview1_celldoubleclick (object sender, DatagridviewcelleveNtargs e) {Btnmod_click (sender, E); }//Save private void Btnsave_click (object sender, EventArgs e) {if (m_dbstate = = DBSTATE.D Smod) {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 ("saved successfully"); } catch (Exception ex) {MessageBox.Show ("Save failed" + 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 ("increased success"); } catch (Exception ex) {MessageBox.Show ("Increase failed" +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 database additions and deletions to verify the case--dataadapter version