我有注册页面,我想检查用户名是否已经存在于数据库中或不在 3 层架构中。
我的注册.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;    
 }
以及textbox 的 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";
     }
   } 
 }
数据访问层:
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;
 }