//Write Login interface logicusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespacelogindatabase{ Public Partial classLogin:form {Private intErrortime =3; PublicLogin () {InitializeComponent (); } Private voidLoginbtn_click (Objectsender, EventArgs e) {Errortime= Errortime-1; stringUsername = TxtName.Text.Trim ();//Remove Account stringPW = TxtPwd.Text.Trim ();//Remove Password stringConstr ="server=.;D ATABASE=SU; Integrated security=true";//Setting the connection stringSqlConnection Mycon =NewSqlConnection (CONSTR);//instantiating a Connection objectMycon. Open (); SqlCommand mycom= Mycon. CreateCommand ();//To create an SQL command execution object stringS1 ="Select Account,password from register where account= '"+ Username +"' and password= '"+ PW +"'";//Writing SQL CommandsMycom.commandtext = S1;//Execute SQL commandSqlDataAdapter Myda =NewSqlDataAdapter ();//instantiating a data adapterMyda.selectcommand = mycom;//to have the adapter execute the SELECT commandDataSet MyDS =NewDataSet ();//instantiating the result data set intn = Myda.fill (MyDS,"Register");//put the result in the data adapter, return the number of tuples if(n! =0) { if(Checkcode.text = =textcheck.text) {MessageBox.Show ("Welcome to use! ");//Login Successful This. Close (); } Else{MessageBox.Show ("Verification Code filling error"); Textcheck.text=""; } } Else if(Errortime <3) {MessageBox.Show ("The user name or password is incorrect. Please re-enter! also have"+ errortime.tostring () +"Second Chance"); txtName.Text="";//Clear AccountTxtpwd.text ="";//clear Password?Txtname.focus ();//the cursor is set on the account}Else{MessageBox.Show ("have you entered a user name or password that has reached three times? will exit the program"); This. Close (); } } Private voidCancelbtn_click (Objectsender, EventArgs e) {application.exit (); } Private voidButton1_Click (Objectsender, EventArgs e) {Register Register=NewRegister (); Register. ShowDialog (); } Private voidCheckcode_click (Objectsender, EventArgs e) {Random random=NewRandom (); intMINV =12345, MAXV =98765; Checkcode.text=Random. Next (MINV, MAXV). ToString (); } }}5. Writing Registration Interface LogicusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacelogindatabase{ Public Partial classRegister:form { PublicRegister () {InitializeComponent (); } Private voidBtnregister_click (Objectsender, EventArgs e) { //Check if it already exists stringUserID = UserId.Text.Trim ();//Remove Account /** * Connect to database*/ stringConstr ="server=.;D ATABASE=SU; Integrated security=true";//Setting the connection stringSqlConnection Mycon =NewSqlConnection (CONSTR);//instantiating a Connection objectMycon. Open (); //querying whether a newly registered user existsSqlCommand checkcmd = mycon. CreateCommand ();//To create an SQL command execution object strings ="Select account from register where account= '"+ UserID +"'"; Checkcmd.commandtext=s; SqlDataAdapter check=NewSqlDataAdapter ();//instantiating a data adapterCheck. SelectCommand = Checkcmd;//to have the adapter execute the SELECT commandDataSet Checkdata =NewDataSet ();//instantiating the result data set intn = check. Fill (Checkdata,"Register");//put the result in the data adapter, return the number of tuples if(n! =0) {MessageBox.Show ("User name exists"); Userid.text=""; Userpw.text =""; Nickname.text=""; } //Confirm Password if(Ensurepw.text! =userpw.text) {Ensurepw.text=""; } //Verification Code if(Textcheck.text! =checkcode.text) {Textcheck.text=""; } //inserting data into SQL logic stringS1 ="INSERT INTO Register (account,password,nickname) VALUES ('"+ Userid.text +"', '"+ Userpw.text +"', '"+ Nickname.text +"')";//Writing SQL CommandsSqlCommand mycom =NewSqlCommand (S1, mycon);//Initialize CommandMycom. ExecuteNonQuery ();//EXECUTE StatementMycon. Close ();//Close Connectionmycom =NULL; Mycon. Dispose (); //Releasing Objects if(Userid.text = =""|| Userpw.textlength <=6|| Nickname.text = =""|| Ensurepw.text = =""|| Textcheck.text = ="") {MessageBox.Show ("please fill in the complete information"); } Else{MessageBox.Show ("Registration Successful"); This. Close (); } } Private voidCheckcode_click (Objectsender, EventArgs e) {Random random=NewRandom (); intMINV =12345, MAXV =98765; Checkcode.text=Random. Next (MINV, MAXV). ToString (); } }
C#_ Connection Database Implementation Login Registration interface