As shown in the interface, when we click on the Save button, the data in the table will be saved to the database to interact with the database.
The first step is to get the data in the table.
stringPWD = textpwd. Text;//Get your password for the first time stringAGEPQD = Txtsurepwd. Text;//Get the password you entered for the second time stringName = Textname. Text;//get the name of the user input intGrendid = Change ();//get user input grade stringPhone = Textphone. Text;//get a user-entered phone stringEmail = Txtemail.text;//get user-entered email
DateTime birthday = time. Value;//get the date of birth entered by the user stringGender =string. Empty;//get the gender of user inputif(boy.) Checked) {Gender="1"; } Else{Gender="0"; }
Because sex is a radio button, getting the code like the one above
In order to be rigorous, we can make a two-time input password before interacting with the database to determine whether the correct
stringPWD = textpwd. Text;//Get your password for the first time stringAGEPQD = Txtsurepwd. Text;//Get the password you entered for the second time if(Agepqd.equals (pwd)) {.........
............... } Else{MessageBox.Show ("two times the password entered is inconsistent!","User Tips", Messageboxbuttons.yesno, messageboxicon.information); }
If the judgment is correct, then we can interact with the database and the code for the database interaction is as follows
stringstr ="data source=.; Initial catalog=myschool;user Id=sa;"; Con=NewSqlConnection (str); Con. Open (); stringsql ="Insert Student values ('"+ pwd +"', '"+ name +"', '"+ Gender +"',"+ Grendid +", '"+ Phone +"', '"+ Birthday +"', '"+ Email +"') SELECT @ @identity"; SqlCommand com=NewSqlCommand (sql, con); intCount = Convert.ToInt32 (com. ExecuteScalar ());
The above code is to open the database channel, and the SQL statement to be executed is committed to the database
Complete code for adding student information is as follows
stringPWD = textpwd. Text;//Get your password for the first time stringAGEPQD = Txtsurepwd. Text;//Get the password you entered for the second time if(Agepqd.equals (pwd)) {stringName = Textname. Text;//get the name of the user input intGrendid = Change ();//get user input grade stringPhone = Textphone. Text;//get a user-entered phone stringEmail = Txtemail.text;//get user-entered emailDateTime birthday = time. Value;//get the date of birth entered by the user stringGender =string. Empty;//get the gender of user input if(boy.) Checked) {Gender="1"; } Else{Gender="0"; } SqlConnection con=NULL; Try { stringstr ="data source=.; Initial catalog=myschool;user Id=sa;"; Con=NewSqlConnection (str); Con. Open (); stringsql ="Insert Student values ('"+ pwd +"', '"+ name +"', '"+ Gender +"',"+ Grendid +", '"+ Phone +"', '"+ Birthday +"', '"+ Email +"') SELECT @ @identity"; SqlCommand com=NewSqlCommand (sql, con); intCount =Convert.ToInt32 (COM. ExecuteScalar ()); if(Count >0) {DialogResult result= MessageBox.Show ("Add success!","User Tips", Messageboxbuttons.yesno, messageboxicon.information); Txtstudentnumber. Text=count. ToString (); if(Result = =dialogresult.yes) { This. Close (); } } } Catch(Exception) {MessageBox.Show ("Add failed!","User Tips", Messageboxbuttons.yesno, messageboxicon.information); } finally { if(Con! =NULL) {con. Close (); } } } Else{MessageBox.Show ("two times the password entered is inconsistent!","User Tips", Messageboxbuttons.yesno, messageboxicon.information); }
Add information to a database from a Windows Form in C # (add student information)