WinForm Database Call (Basic method)

Source: Internet
Author: User

//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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.