【问题标题】:I want to check the username is already exists in my database table or not?我想检查用户名是否已经存在于我的数据库表中?
【发布时间】:2013-03-31 16:51:46
【问题描述】:

我有注册页面,我想检查用户名是否已经存在于数据库中或不在 3 层架构中。

MyRegistration.cs:

public static int checkusername(string user_txt)
 {
  int id2 = 0;
  string selectstr = "select * from xyz where UserName = '" + user_txt + " ' ";
  id2 = DataAccessLayer.ExecuteReader(selectstr);
  return id2;    
 }

以及文本框的onclick事件背后的代码:

protected void txt_username_TextChanged(object sender, EventArgs e)
 {
  if (!string.IsNullOrEmpty(txt_username.Text))
   {
    int id = xyz.checkusername(txt_username.Text.Trim());
    if (id > 0)
     {
      lblStatus.Text = "UserName Already Taken";
     }
    else
     {
      lblStatus.Text = "UserName Available";
     }
   } 

 }

DataAccessLayer:

public static int ExecuteReader(string Query)
 {
  SqlConnection con = new SqlConnection();
  con.ConnectionString = GetConnectionString();
  con.Open();
  int id = 0;            
  SqlCommand cmd = new SqlCommand();
  cmd.CommandText = Query;
  cmd.CommandType = System.Data.CommandType.Text;
  cmd.Connection = con;
  SqlDataReader reader = cmd.ExecuteReader();
  while (reader.Read())
   {
    id++;
   }
  cmd = null;
  reader.Close();
  con.Close();
  return id;
 }

【问题讨论】:

  • 那么这里有什么疑问???
  • 谨防 SQL 注入!
  • 你打开了一些这样的安全风险,你需要防止 SQL 注入。编辑:看起来keyshapener打败了我!
  • 那有什么不好的呢?
  • 这是代码不起作用我认为 onclick 事件有问题你能再给我一个代码吗?

标签: c# asp.net 3-tier


【解决方案1】:

我已经编辑了您的一些代码,尝试如下...它将帮助您...

文字变化事件:

    protected void txt_username_TextChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txt_username.Text))
        {
            if (xyz.checkusername(txt_username.Text.Trim()))
            {
                lblStatus.Text = "UserName Already Taken";
            }
            else
            {
                lblStatus.Text = "UserName Available";
            }
        }

    }

检查用户名:

    public bool CheckUsername(string user_txt)
    {
        bool Result;
        Result = DataAccessLayer.ExecuteReader(user_txt);
        return Result;
    }

执行阅读器:

    public bool ExecuteReader(string user_txt)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = GetConnectionString();
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from xyz where UserName = @UserID", con);
        SqlParameter param = new SqlParameter();
        param.ParameterName = "@UserID";
        param.Value = user_txt;
        cmd.Parameters.Add(param);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.HasRows)
            return true;
        else
            return false;
    }

【讨论】:

    【解决方案2】:

    通常,如果“选择查询”没有找到带有参数 user_txt 的用户名,您的 id2 最终将得到值 -1。所以适当的代码是:

               if (id ==-1)
                   {
                       lblStatus.Text = "UserName Available";
                   }
               if (id>0)
                   {
    
                       lblStatus.Text = "UserName Already Taken";
                   }
    

    顺便说一句,您的代码非常不安全,并且您的数据库很容易使用SQL Injection 进行攻击我建议您了解这个问题并在查询中添加参数以防止它。 C# 有它的方法来实现这一点。不要试图修复对数据库的访问,从头开始,牢记 SQL 注入。

    【讨论】:

    • 您需要像其他 cmets 所说的那样更正“selectstr”
    【解决方案3】:

    正如其他人所提到的,这种方法存在潜在的严重安全问题。

    但是,您的问题可能出在user_txt + " ' "。第二个' 周围的空格,特别是它之前的空格,可能会导致用户名与预期不匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2019-08-04
      • 2013-12-16
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多