Connecting to a database

Source: Internet
Author: User

1  protected voidBtnlogin_click (Objectsender, EventArgs e)2         {3             #regionFirst version4             stringUserName = This. TxtUserName.Text.Trim ();5             stringPassWord = This. TxtPwd.Text.Trim ();//remove whitespace from the end of a string6             //Get user Information7             if(rbtnocard.checked = =true)8             {9                 //string strconn = configurationmanager.connectionstrings["lecturecheckedconnectionstring"]. ConnectionString;Ten                 stringstrconn ="server= (local); Initial catalog=change_skj;integrated Security=sspi";//You can also connect to the database in the above manner, but you must set the appsettings in the Web. config file One                 using(SqlConnection conn =NewSqlConnection (strconn)) A                 { - Conn. Open (); -                     using(SqlCommand cmd = conn.) CreateCommand ())//executing SQL statements is tantamount to SqlCommand cmd = new SqlCommand (SQL statement, database connection); the                     { -Cmd.commandtext ="SELECT * from T_cyuser where [email protected] and [email protected]"; -Cmd. Parameters.addwithvalue ("Nocard", userName);//Add a parameter to the @nocard in the previous SELECT statement with a value of username, which is the user name entered -Cmd. Parameters.addwithvalue ("PSW", PassWord); +                         using(SqlDataReader dataReader = cmd.) ExecuteReader ())//ExecuteReader () executes the command, returning a typed IDataReader -                         { + Datareader.read (); A                             if(datareader.hasrows)//determine whether a user is authenticated by including rows in DataReader at                             { -session["Card"] =1; -session[" Number"] =TxtUserName.Text.Trim (); -                                 //if ((int) datareader["Attr"] = = 1) -                                 //{ -                                 //session["attr"] = 1; in                                 //session["idmg"] = Convert.ToInt32 (datareader["ID"]); -                                 //Response.Redirect ("~/teacher/teacher.aspx"); to                                 //} +                                 //else if ((int) datareader["Attr"] = = 0) -                                 //{ the                                 //Response.Redirect ("~/user/student.aspx"); *                                 //} $                                 if((int) datareader["Attr"] ==2)Panax Notoginseng                                 { -session["attr"] =2; thesession["IDMG"] = Convert.ToInt32 (datareader["ID"]); +Response.Redirect ("~/superadmin/superadmin.aspx"); A                                 } the                                 Else +                                 { -Response.Write ("<script>alert (' Login failed! Please check your username or password '); location= ' Login.aspx ' </script>");//use alert in ASP. To prompt for error messages $                                 } $                             } -                             Else -                             { theResponse.Write ("<script>alert (' Login failed! Please check your username or password '); location= ' Login.aspx ' </script>");//use alert in ASP. To prompt for error messages -                             }Wuyi                         } the                     } -  Wu                 } -             } About             Else if(rbtnouser.checked = =true) $             { -                 //string strconn = configurationmanager.connectionstrings["lecturecheckedconnectionstring"]. ConnectionString; -                 stringstrconn ="server= (local); Initial catalog=change_skj;integrated Security=sspi"; -                 using(SqlConnection conn =NewSqlConnection (strconn)) A                 { + Conn. Open (); the                     using(SqlCommand cmd =Conn. CreateCommand ()) -                     { $Cmd.commandtext ="SELECT * from T_cyuser where [email protected] and [email protected]"; theCmd. Parameters.addwithvalue ("Nouser", userName); theCmd. Parameters.addwithvalue ("PSW", PassWord); the                         using(SqlDataReader DataReader =cmd. ExecuteReader ()) the                         { - Datareader.read (); in                             if(datareader.hasrows)//determine whether a user is authenticated by including rows in DataReader the                             { thesession["Card"] =NULL; Aboutsession[" Number"] =TxtUserName.Text.Trim (); the                                 if((int) datareader["Attr"] ==1) the                                 { thesession["attr"] =1; +session["IDMG"] = Convert.ToInt32 (datareader["ID"]); -Response.Redirect ("~/teacher/teacher.aspx"); the                                 }Bayi                                 Else if((int) datareader["Attr"] ==0) the                                 { theResponse.Redirect ("~/user/student.aspx"); -                                 } -                                 Else  the                                 { theResponse.Write ("<script>alert (' re-select Identity '); location= ' Login.aspx ' </script>"); the                                 } the                             } -                             Else the                             { theResponse.Write ("<script>alert (' Login failed! Please check your username or password '); location= ' Login.aspx ' </script>"); the                             }94                         } the                     } the  the                 }98             } About             #endregion -}

Connecting to a database

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.