namespaceaccess{ Public Partial classForm1:form {//to define a connection path to a database stringTxtconn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\access.mdb"; PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { //Modify the Record button code using(OleDbConnection conn =NewOleDbConnection (Txtconn)) { stringsql =string. Format ("Update table1 Set fl= ' {1} ', lxfs= ' {2} ', dzyj= ' {3} ' where xm= ' {0} '", TextBox1.Text, TextBox2.Text, TextBox3.Text, Textbox4.text); OleDbCommand cmd=NewOleDbCommand (SQL, conn); Conn. Open (); Cmd. ExecuteNonQuery (); } } Private voidButton2_Click (Objectsender, EventArgs e) { //Delete Record button code using(OleDbConnection conn =NewOleDbConnection (Txtconn)) { stringsql =string. Format ("Delete from table1 where xm= ' {0} '", TextBox1.Text); OleDbCommand cmd=NewOleDbCommand (SQL, conn); Conn. Open (); Cmd. ExecuteNonQuery (); } } Private voidButton3_Click (Objectsender, EventArgs e) { //Add Record button code using(OleDbConnection conn =NewOleDbConnection (Txtconn)) { stringsql =string. Format ("INSERT INTO table1 (xm,fl,lxfs,dzyj) VALUES (' {0} ', ' {1} ', ' {2} ', ' {3} ')", TextBox1.Text, TextBox2.Text, TextBox3.Text, Textbox4.text); OleDbCommand cmd=NewOleDbCommand (SQL, conn); Conn. Open (); Cmd. ExecuteNonQuery (); } } }}
C # Most basic database additions and deletions