The general landing interface, is to use the user name and password in the database matching relationship, to achieve the landing of the jump function.
First, the design of the User data table is introduced.
Where the ID column needs to be set up for incremental identification, as the user increases, the value of the ID is incremented to avoid duplication.
Then there is the action class for the database in C #.
1 classDataBase2 {3 Public Static stringlogin_id ="";4 Public Static stringLogin_name ="";5 Public StaticSqlConnection My_conn;6 Public Static stringOpenconnstr =@"Data SOURCE=MHZHANG\SQLEXPRESS;DATABASE=DB_ZMH; User id=xxxx; Pwd=xxxxxxxx";7 8 Public StaticSqlConnection Getcon ()9 {TenMy_conn =NewSqlConnection (OPENCONNSTR); One My_conn.open (); A returnMy_conn; - } - the Public voidCon_open () - { - Getcon (); - } + - + Public voidconn_close () A { at if(My_conn.state = =ConnectionState.Open) - { - my_conn.close (); - my_conn.dispose (); - } - } in - PublicSqlDataReader GETSDR (stringsqlstr) to { + Getcon (); -SqlCommand my_com =My_conn.createcommand (); theMy_com.commandtext =sqlstr; *SqlDataReader My_reader =my_com. ExecuteReader (); $ returnMy_reader;Panax Notoginseng } - the Public voidDosqlcom (stringsqlstr) + { A Getcon (); theSqlCommand sqlcom =NewSqlCommand (Sqlstr, my_conn); + sqlcom. ExecuteNonQuery (); - sqlcom. Dispose (); $ conn_close (); $ } - - PublicDataSet Getds (stringSQLSTR,stringtableName) the { - Getcon ();WuyiSqlDataAdapter Sqlda =NewSqlDataAdapter (Sqlstr, my_conn); theDataSet My_dataset =NewDataSet (); - Sqlda. Fill (My_dataset, tableName); Wu conn_close (); - returnMy_dataset; About } $ - -}
The way the database is linked to the individual is different, for informational purposes only.
Finally, the code implementation of the login form:
1 Public Partial classForm_login:form2 {3DataBase UserDB =NewDataBase ();4 PublicForm_login ()5 {6 InitializeComponent ();7 }8 9 Private voidBtn_login_click (Objectsender, EventArgs e)Ten { One if(Tb_username.text! =""& Tb_userpwd.text! ="") A { -SqlDataReader TEMDR = USERDB.GETSDR ("SELECT * from tb_userlist where username= '"+ Tb_UserName.Text.Trim () +"' and userpwd= '"+ Tb_UserPwd.Text.Trim () +"'"); - BOOLifcom =TEMDR. Read (); the if(ifcom) - { -Form_main frmmain =NewForm_main (); - frmmain.show (); + This. Hide (); - } + Else A { atMessageBox.Show ("The user name or password is wrong! "); -Tb_username.text =""; -Tb_userpwd.text =""; - } - userdb.conn_close (); - } in Else -MessageBox.Show ("please fill in the User name and password! "); to + } - the Private voidForm_login_load (Objectsender, EventArgs e) * { $ TryPanax Notoginseng { - Userdb.con_open (); the userdb.conn_close (); +Tb_username.text =""; ATb_userpwd.text =""; the } + Catch - { $MessageBox.Show ("Database connection Error! "); $ application.exit (); - } - } the - Private voidForm_login_activated (Objectsender, EventArgs e)Wuyi { the Tb_username.focus (); - } Wu - Private voidBtn_quit_click (Objectsender, EventArgs e) About { $ application.exit (); - } -}
There are other ways to judge the way a user exists in a data table by using SqlDataReader.
C # Foundation--Implementation of system login function