You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET中如何根据登录用户ID匹配Tasks表数据并显示到ListBox

解决登录用户任务显示问题

问题分析

  • 登录代码仅存储了username到Session,未存储关键的UserID,导致后续无法匹配用户专属任务
  • 当前Tasks查询语句未过滤用户,会查询所有任务,效率低且不符合需求
  • SQL语句使用字符串拼接存在SQL注入风险,必须改用参数化查询

修复步骤

1. 修复登录代码,存储UserID到Session

修改登录按钮逻辑,从查询结果中提取UserID并存入Session:

protected void btnLogin_Click(object sender, EventArgs e)
{
    Page.Validate();
    // 参数化查询避免SQL注入
    string query = "SELECT UserID, UserName FROM tblUser WHERE UserName = @UserName AND Password = @Password AND Usertype = @Usertype";
    using (SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-PIERKI5\SQLEXPRESS;initial Catalog=LoginDb;integrated Security=True;"))
    {
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            // 添加参数
            cmd.Parameters.AddWithValue("@UserName", txtName.Text.Trim());
            cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
            cmd.Parameters.AddWithValue("@Usertype", DropDownList1.SelectedItem.Text);
            
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            
            if (dt.Rows.Count > 0)
            {
                // 存储UserID和用户名到Session
                Session["UserID"] = dt.Rows[0]["UserID"].ToString();
                Session["username"] = txtName.Text.Trim();
                
                if (DropDownList1.SelectedIndex == 0) // admin
                {
                    Response.Redirect("Default_admin.aspx");
                }
                else if (DropDownList1.SelectedIndex == 1) // user
                {
                    Response.Redirect("Default_user.aspx");
                }
            }
        }
    }
}

2. 修改任务列表加载代码,只查询当前用户的任务

直接在SQL中过滤当前登录用户的UserID,简化逻辑同时提升效率:

protected void Page_Load(object sender, EventArgs e)
{
    // 校验登录状态,未登录则跳转登录页
    if (Session["UserID"] == null)
    {
        Response.Redirect("Login.aspx");
        return;
    }
    
    // 仅首次加载页面时绑定数据,避免重复绑定
    if (!IsPostBack)
    {
        string query = "SELECT taskName FROM Tasks WHERE UserID = @UserID";
        using (SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-PIERKI5\SQLEXPRESS;initial Catalog=LoginDb;integrated Security=True;"))
        {
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                cmd.Parameters.AddWithValue("@UserID", Session["UserID"].ToString());
                
                SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adpt.Fill(dt);
                
                // 直接绑定DataTable到ListBox,简化代码
                TasksListBox.DataSource = dt;
                TasksListBox.DataTextField = "taskName";
                TasksListBox.DataBind();
            }
        }
    }
}

关键优化点

  • 使用using语句自动释放数据库连接资源,避免连接泄漏
  • 采用参数化查询彻底规避SQL注入攻击
  • 仅在首次加载页面时绑定数据,减少不必要的数据库请求
  • 增加Session校验,防止未登录用户直接访问任务页面

内容的提问来源于stack exchange,提问作者user13188123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 17:27:25