【发布时间】: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 事件有问题你能再给我一个代码吗?