Joke Collection Website - Cold jokes - How to verify account number and password with database?

How to verify account number and password with database?

1, first establish a database UserTest.

2. Create a table named UsersTable, design this table and create three columns: u_id, u_name and u_pwd.

3. u_id is an int data type, and1is added; U_name is nvarchar type, and its length is12; U_pwd is of varchar type with length of 12, and then save the table.

4. Open the table and enter "abc" in the first line of u_name and "abc" in u_pwd. Close the form.

5. Open VS and create a new Web project. Double-click the default.aspx page, and then

HTML code

& lt! -replace->

& ltbody style="margin: 0px " >

& ltform id = " form 1 " runat = " server " >

& lttable border = " 0 " cell padding = " 0 " cellspacing = " 0 " style = " width: 100%; Height: 50px ">

& lttr & gt

& ltTD style = " vertical-align:middle; Width: 50%; Text alignment: centered; Font thickness: bold; Font size: 25px color: # 003366; Font series: Arial”& gt Login demonstration of Yun Lei Zhilian.

& ltASP:Panel ID = " pnl login " runat = " server " Height = " 30px " Width = " 100% " & gt;

User name:

& lt! -For the Password text box, the TextMode property should be set to password, so that the input value will appear in the form of * * * *, which can protect the password from being seen by others when it is input->

& ltASP:TextBox ID = " txbUserPassword " runat = " server " text mode = " Password " & gt; & lt/ASP:TextBox & gt;

& ltASP:Button ID = " btnLogin " Runat = " server " text = " log in " OnClick = " btnLogin _ Click "/& gt; & lt/ASP:Panel & gt;

& ltASP:Panel ID = " pnl welcome " runat = " server " Height = " 30px " Width = " 100% " & gt;

& ltASP:Label ID = " LBL welcome " runat = " server " & gt; & lt/ASP:Label & gt;

& ltASP:Button ID = " BTN quit " Runat = " server " causes validation = " false " text = " exit " width = " 146 px " onclick = " BTN quit _ click "/> & lt; /ASP:Panel & gt;

& lt/TD & gt;

& lt/tr & gt;

& lt/table & gt;

& lttable border = " 0 " cell padding = " 0 " cellspacing = " 0 " style = " width: 100%; Height: 100px ">

& lttr & gt

& ltTD style = " vertical-align:middle; text-align: center " >

& lt! -This information is used to show whether the login was successful or not->

& ltASP:Label ID = " LBL message " runat = " server " ForeColor = " # c 00000 " >& lt/ASP:Label & gt; & lt/TD & gt;

& lt/tr & gt;

& lt/table & gt;

& lt/form & gt;

& lt/body & gt;

& lt! -End of replacement->

6. Press F7 to enter the code view and write the following code in Page_Load ():

C# code

Protected void Page_Load (object sender, EventArgs e)

{

//Make sure that the page is visited for the first time.

If (! Page. IsPostBack)

{

CheckPageStatus();

}

}

//Check whether the page is logged in by judging whether the Session["UserName"] is empty (if the login is successful, a value will be injected for the Session["UserName"], which will not be empty). . Otherwise, it is regarded as not logged in).

Private void CheckPageStatus ()

{

If (session ["user name"]! = empty)

{

pnlLogin。 Visible = false;

Welcome to visit. Visible = true;

LblWelcome。 Text = "Welcome to login," +session ["username"]. Tostring ()+ "Comrade";

}

other

{

Welcome to visit. Visible = false;

pnlLogin。 Visible = true;

}

//This is used to receive information after login or logout. Personal habits, you don't have to do this.

if (Session["Message"]! = empty)

{

lblMessage。 Text = Session["Message"]。 ToString();

Conversation. Remove ("message");

}

other

lblMessage。 Text =

}

7. Let's finish it. Press F5 to run and see the effect. You will find that when you are not logged in, the "Exit" section is not displayed. It's amazing.

8. Then close this IE page and press "Design" in the lower left corner of VS to switch to the design view.

9. Double-click the login button in the design view and write the following login code for it:

C# code

Protected void btnLogin_Click (object sender, event parameter e)

{

//The following login methods are just my habit. I often use DataTable to do everything. I suggest you don't rely on it … otherwise you will be laughed at … there are many good ways to log in, just refer to it.

string str connection = " SERVER =(local); DATABASE = UserTestUID = saPWD = "; //PWD= Write the sa password of your SQL SERVER at the back.

System. Data.sqlclient.sqlconnectioncn = new system. data . sqlclient . sqlconnection(str connection);

//SQL statement? There is nothing you can do. Just copy it.

string strSql = " SELECT * FROM UsersTable WHERE u _ name = ' "+txbUserName。 Text+"'and u_pwd='"+txbUserPassword. text+" ' ";

//What's the use of //SqlDataAdapter? I suggest you go to MSDN.

System. Data.sqlclient.sqldataadapterda = new system. data . sqlclient . sqldata adapter(strSql,cn);

System. data . DataTable dt = new DataTable();

Da. Fill (dt);

If (dt. Number of rows. Count & gt0)

{

Session ["username"] = dt. Line [0]["u_name"]. ToString();

Session["Message"] = "Login successful!" ;

Response. redirect(" default . aspx ");

}

other

{

Session ["Message"] = "Login failed. Please login again. ;

Response. redirect(" default . aspx ");

}

}

10, then press F5 to test, enter 123 in the user name and password, and press the "Login" button, which will prompt that the login failed. If you enter abc, you will be prompted to log in successfully. Because abc is the only user in the database.

1 1, close the IE page, and finally we quit.

12, return to the design view, and double-click the "Exit" button to write the following code for it:

C# code

Protected void btnQuit_Click (object sender, EventArgs e)

{

If (session ["user name"]! = empty)

{

Conversation. Remove ("user name");

Session ["Message"] = "Exit succeeded. Welcome to visit again ";

Response. redirect(" default . aspx ");

}

}

13, press F5 to run. After logging in successfully, press the "Exit" button, and you will be prompted that you have exited.

14, in order to verify whether the user has logged in, you need to add:

C# code

If (session ["user name"]! = empty)

//Users can access.

other

//If the user cannot access it, throw it back to the login page: Response. redirect(" log in . aspx ");