Add information to a database from a Windows Form in C # (add student information)

Source: Internet
Author: User

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)

Related Article

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.