如何使用会话从数据库中获取特定数据?

How to get a specific data from your database using Session?

我想在登录时从我的数据库中获取特定数据。我网页的默认登录用户名是学生 ID。示例 (studentid="2011018921") 这是我在登录时声明的用户名,但是我想在登录系统时在我的数据库中获取特定数据,就像我想获取学生的名字一样。

此处的代码获取用户名的数据。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using MSSQLConnector;
using System.Data;

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class OnlineAppSyss : System.Web.UI.Page
    {
        private MSConnector connector = new MSConnector();
        string query = null;
        string clear = "";
        int rowcounter = 0;
        private DataSet selectedData;

        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            connector.ConnectionString = "Data Source=keith;Initial Catalog=Student;Integrated Security=True";

            string username = (this.UserName.Value);
            string pass = (this.Password.Value);

            query = "select studentid,password from student";

            selectedData = connector.ExecuteQuery(query);


            for (;;)
            {
                string userid = selectedData.Tables[0].Rows[rowcounter]["StudentID"].ToString();
                string password = selectedData.Tables[0].Rows[rowcounter]["Password"].ToString();
                //Username and Password are correct
                if ((username == userid) && (pass == password))
                {
                    //This session Login I want to get the firstname of the student
                    Session["login"] = userid;

                    Response.Redirect("StudentPage.aspx", true);
                    break;
                }
                //admin side, correct username and password
                else if ((username == "admin") && (pass == "cmpe1234"))
                {
                    Response.Redirect("AdministratorPage.aspx");
                }
                //admin side, correct username invalid password
                else if ((username == "admin") && (pass != "cmpe1234"))
                {
                    Response.Write("<script language=javascript>alert('Username and password does not match. Try again1');</script>");
                    break;
                }
                //empty username and password
                else if (((username == "") && (pass == "")) || ((userid == null) && (password == null)))
                {
                    Response.Write("<script language=javascript>alert('Username and password must have input value. Try again2');</script>");
                    break;
                }
                //username is invalid and password is correct ; username is correct and password is invalid
                else if (((username != userid) && (pass == password)) || ((username == userid) && (pass != password)))
                {
                    Response.Write("<script language=javascript>alert('Username and password does not match. Try again');</script>");
                    break;
                }
                else
                {
                    rowcounter++;
                }
            }
        }
    }
}

这是我在后面的另一个 aspx 代码中检查我的用户名以查看 studentid 日志的地方。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class StudentPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["login"]==null)
            {

            }
            else
            {
                //it states here (Welcome 2011018921)
                labelsession.Text = "Welcome  " + Session["login"].ToString();
            }
        }
    }
}

如何将我的代码更改为 state (Welcome Keith) 而不是将 studentID 声明为 FirstName。

我想这与此处的代码有关?

        string userid = selectedData.Tables[0].Rows[rowcounter]["StudentID"].ToString();
        string password = selectedData.Tables[0].Rows[rowcounter]["Password"].ToString();
        //Username and Password are correct
        if ((username == userid) && (pass == password))
        {
            Session["login"] = userid;

            Response.Redirect("StudentPage.aspx", true);
            Response.Write("<script language=javascript>alert('Login Successful');</script>");
            break;
        }

如何在不将用户名身份验证更改为 (studentID) 的情况下声明会话以显示我的数据的名字?

这不是一个好的身份验证方式,但根据您的要求,将您的代码修改为: 将您的查询更改为

query = "select firstname,studentid,password from student";

然后修改你的 for 循环,将上面的代码替换为:

            string userid = selectedData.Tables[0].Rows[rowcounter]["StudentID"].ToString();
            string password = selectedData.Tables[0].Rows[rowcounter]["Password"].ToString();
            string firstname = selectedData.Tables[0].Rows[rowcounter]["FirstName"].ToString();
            if ((username == userid) && (pass == password))
            {
                //This session Login I want to get the firstname of the student
                Session["login"] = userid;
                Session["firstname"] = firstname ;
                Response.Redirect("StudentPage.aspx", true);
                break;
            }

在任何地方使用名字