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 testdboper{public partial class Fmmain:form {static string connstr = Configurationmanager.conne ctionstrings["ConnStr"]. ConnectionString; Static Dbstate dbs; Enum Dbstate {sadd, Smod, Sdel, Sbro} public Fmmain () {InitializeComponent (); private void Form1_Load (object sender, EventArgs e) {this.dataGridView1.AutoGenerateColumns = False Do not automatically generate column Btnquery_click (sender, E); private void Btnexit_click (object sender, EventArgs e) {this. Close (); }//query private void Btnquery_click (object sender, EventArgs e) {int p = 0; if (This.dataGridView1.CurrentCell! = null) {p = this.dataGridView1.CurrentCell.RowIndex; } OleDbConnection conn = new OleDbConnection (CONNSTR); OleDbDataAdapter da = new OleDbDataAdapter ("SELECT * from the users where username like '%" + this.textBox1.Text + "% '", conn) ; DataTable dt = new DataTable (); Conn. Open (); Da. Fill (DT); This.dataGridView1.DataSource = DT; if (P < dataGridView1.Rows.Count) {This.dataGridView1.CurrentCell = Datagridview1.rows[p]. Cells[0]; } toolstripstatuslabel1.text = "Total query to" + dt. Rows.Count.ToString () + "bar data"; DBS = Dbstate.sbro; SETBTN (); }//Added private void btnAdd_Click (object sender, EventArgs e) {dbs = Dbstate.sadd; SETBTN (); Usertxtclear (); }//Modify private void Btnmod_click (object sender, EventArgs e) {dbs = Dbstate.smod; SETBTN (); UserrefResh (); }//delete private void Btndel_click (object sender, EventArgs e) {if (MessageBox.Show ("You are sure you want to delete In addition to it? "," hint ", messageboxbuttons.yesno)! = Dialogresult.yes) {return;} String sql = "Delete from users where ID [email protected]"; OleDbConnection conn = new OleDbConnection (CONNSTR); OleDbCommand cmd = new OleDbCommand (SQL, conn); oledbparameter[] param = {new OleDbParameter ("@id", This.datagridview1.rows[this.datagridview1.currentcell.rowindex ]. cells["id"]. Value.tostring ())}; Cmd. Parameters.addrange (param); Conn. Open (); int n = 0; n = cmd. ExecuteNonQuery (); Conn. Close (); if (n! = 0) {MessageBox.Show ("delete succeeded"); } else {MessageBox.Show ("delete failed"); } btnquery_click (sender, E); }//Save private void Btnsave_click (object sender,EventArgs e) {if (TxtUserName.Text.Trim () = = "") {MessageBox.Show ("name cannot be empty"); Return } if (TxtUserSex.Text.Trim () = = "") {MessageBox.Show ("Gender cannot be null"); Return } if (TxtUserAge.Text.Trim () = = "") {MessageBox.Show ("Age cannot be empty"); Return } if (dbs = = dbstate.sadd) {String sql = "INSERT INTO Users (username,userage,userse x) VALUES (@username, @userage, @usersex) "; OleDbConnection conn = new OleDbConnection (CONNSTR); OleDbCommand cmd = new OleDbCommand (SQL, conn); oledbparameter[] param ={New OleDbParameter ("@username", txtUsername.Text), new OLE DB Parameter ("@userage", Txtuserage.text), New OleDbParameter ("@usersex", Txtusersex.text) }; Cmd. Parameters.addrange (param); Conn. Open (); int n = 0; n = cmd. ExecuteNonQuery (); Conn. Close (); if (n! = 0) {MessageBox.Show ("add success"); } else {MessageBox.Show ("Increase failed"); }} else if (dbs = = dbstate.smod) {String sql = "Update users set [email& Nbsp;protected],[email protected],[email protected] where id= @id "; OleDbConnection conn = new OleDbConnection (CONNSTR); OleDbCommand cmd = new OleDbCommand (SQL, conn); oledbparameter[] param ={New OleDbParameter ("@username", txtUsername.Text), new O Ledbparameter ("@userage", Txtuserage.text), New OleDbParameter ("@usersex", txtusers EX.text), New OleDbParameter ("@id", This.datagridview1.rows[datagridview1.currentce ll. RowIndex]. cells["id"]. Value.tostring ())}; Cmd. Parameters.addrange (param); Conn. Open (); int n = 0; n = cmd. ExecuteNonQuery (); Conn. Close (); if (n! = 0) {MessageBox.Show ("modified successfully"); } else {MessageBox.Show ("failed to modify"); }} dbs = Dbstate.sbro; SETBTN (); Btnquery_click (sender, E); }//Cancels private void Btncancel_click (object sender, EventArgs e) {dbs = Dbstate.sbro; SETBTN (); }//----------------------------------------------function------------------------------------------------//clear Empty input box private void usertxtclear () {txtusername.text = ""; Txtusersex.text = ""; Txtuserage.text = ""; }//Refresh user private void Userrefresh () {txtUsername.Text = This.datagridview1.rows[datagridv Iew1. Currentcell.rowindex]. cells["username"]. Value.tostring (); Txtusersex.text = This.datagridview1.rows[datagridview1.currentcell.rowindex]. cells["Usersex"]. Value.tostring (); Txtuserage.text = This.datagridview1.rows[datagridview1.currentcell.rowindex]. cells["Userage"]. Value.tostring (); }//Set button state private void Setbtn () {btnadd.enabled = dbs = = Dbstate.sbro; btnmod.enabled = DBS = = Dbstate.sbro; btndel.enabled = DBS = = Dbstate.sbro; btnquery.enabled = DBS = = Dbstate.sbro; btnsave.enabled = dbs! = Dbstate.sbro; btncancel.enabled = dbs! = Dbstate.sbro; private void Datagridview1_doubleclick (object sender, EventArgs e) {Btnmod_click (sender, E); } }}
C#winfrom database additions and deletions to verify the case--sql operation version