//New data to the database
Private voidButton1_click_1 (Objectsender, EventArgs e) {SqlConnection conn=NewSqlConnection ("server=.; database= work; user=sa;pwd=123"); SqlCommand cmd=Conn. CreateCommand (); Cmd.commandtext="INSERT into student values (@sno, @sname, @ssex, @sbirthday, @class)"; Cmd. Parameters.clear (); Cmd. Parameters.Add ("@sno", TextBox1. Text.trim ()); Cmd. Parameters.Add ("@sname", TextBox2. Text.trim ()); if(Rbtnan. Checked) {cmd. Parameters.Add ("@ssex","male"); } Else{cmd. Parameters.Add ("@ssex","female"); } cmd. Parameters.Add ("@sbirthday", Datetimepicker1.value); Cmd. Parameters.Add ("@class", ComboBox1.Text.Trim ()); Conn. Open (); intCount =cmd. ExecuteNonQuery (); if(Count >0) {MessageBox.Show ("successful execution, the number of rows affected is:"+count. ToString ()); } Else{MessageBox.Show ("Execution failed! "); } conn. Close (); }
//Modify data in the database
Private voidButton1_Click (Objectsender, EventArgs e) {SqlConnection conn=NewSqlConnection ("server=.; database= work; user=sa;pwd=123"); SqlCommand cmd=Conn. CreateCommand (); Cmd.commandtext="Update student Set [email protected],[email protected],[email protected],[email protected] where [email protected]
"; Cmd. Parameters.clear (); Cmd. Parameters.Add ("@sno", TextBox1. Text.trim ()); Cmd. Parameters.Add ("@sname", TextBox2. Text.trim ()); if(Rbtnan. Checked) {cmd. Parameters.Add ("@ssex","male"); } Else{cmd. Parameters.Add ("@ssex","female"); } cmd. Parameters.Add ("@sbirthday", Datetimepicker1.value); Cmd. Parameters.Add ("@class", ComboBox1.Text.Trim ()); Conn. Open (); intCount =cmd. ExecuteNonQuery (); if(Count >0) {MessageBox.Show ("successful execution, the number of rows affected is:"+count. ToString ()); } Else{MessageBox.Show ("Execution failed! "); } conn. Close (); }
//delete data from the database
Private voidButton1_Click (Objectsender, EventArgs e) {SqlConnection conn=NewSqlConnection ("server=.; database= work; user=sa;pwd=123"); SqlCommand cmd=Conn. CreateCommand (); Cmd.commandtext="Delete from student where [email protected]"; Cmd. Parameters.clear (); Cmd. Parameters.Add ("@sno", TextBox1. Text.trim ()); Conn. Open (); intCount =cmd. ExecuteNonQuery (); if(Count >0) {MessageBox.Show ("successful execution, the number of rows affected is:"+count. ToString ()); } Else{MessageBox.Show ("Execution failed! "); } conn. Close (); }
//Querying data in the database
Private voidButton1_Click (Objectsender, EventArgs e) {SqlConnection conn=NewSqlConnection ("server=.; database= work; user=sa;pwd=123"); SqlCommand cmd=Conn. CreateCommand (); DataTable DT=NewDataTable (); CMD=Conn. CreateCommand (); Cmd.commandtext="SELECT * FROM Student"; Conn. Open (); SqlDataReader Dr=cmd. ExecuteReader (); for(inti =0; I < Dr. FieldCount; i++) {dt. Columns.Add (Dr. GetName (i)); } while(Dr. Read ()) {DataRow row=dt. NewRow (); row["Sno"] = dr["Sno"]; row["sname"] = dr["sname"]; row["Ssex"] = dr["Ssex"]; row["Sbirthday"] = dr["Sbirthday"]; Dt. Rows.Add (row); } conn. Close (); }
WinForm Database Call (Basic method)