重复用户名验证 [英] dupicate username validation
本文介绍了重复用户名验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了2页登录名和密码。我正在检查用户名的重复输入。
i已经为它编写了查询。但是如果用户名是相同的,如何阻止它插入.i已经尝试了进一步,但这是错误的。请帮助
这里是我的代码 -
i have created 2 pages login and password.and i am checking for duplicate entry of username.
i have written query for it.But how can prevent it from inserting if the username is same.i have tried further but it is wrong.pls help
here is my code-
protected void signupbtn_Click(object sender, EventArgs e)
{
try
{
conn.Open();
string st;
st = "select count(username)from login where username='" + txtname.Text + "'";
cmd = new SqlCommand(st, conn);
if (st > 0)
{
Response.Write("duplicate record");
}
else
{
cmd = new SqlCommand("insert into login(name,midname,surname,username,password,contact,dob,email,address,occupation,ltype) values('" + txtfirst.Text + "','" + txtmid.Text + "','" + txtsur.Text + "','" + txtname.Text + "','" + txtpass.Text + "','" + txtcontact.Text + "','" + txtdob.Text + "','" + txtemail.Text + "','" + txtaddr.Text + "','" + txtocc.Text + "','" + typeButtonList1.SelectedValue + "')", conn);
cmd.ExecuteNonQuery();
Response.Write("Record inserted");
}
}
推荐答案
注意 SQL_injection [ ^ ]因为在c#中构建查询是不安全的..
尝试这样..
Be aware of SQL_injection[^] as it is not safe to frame the query in c#..
try like this..
conn.Open();
string st;
st = "select count(username)from login where username='" + txtname.Text + "'";
var cmd = new SqlCommand(st, conn);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
int count = Convert.ToInt32(dt.Rows[0][0]);
if (count >= 1)
{
Response.Write("duplicate record");
}
首先,你听说过 SQL注入 [ ^ ]?应该以这样的方式创建ASP.NET应用程序,保护sql注入攻击 [< a href =http://msdn.microsoft.com/en-us/library/ff648339.aspxtarget =_ blanktitle =New Window> ^ ]。
我强烈建议使用存储过程并从后面的代码中调用它[< a href =http://support.microsoft.com/kb/306574/entarget =_ blanktitle =New Window> ^ ]。如果您想避免登录重复,请将此字段设置为唯一 [ ^ ]!
如何:创建和修改UNIQUE约束 [ ^ ]
First of all, have you ever heard about SQL injection[^]? An ASP.NET application should be created in that way which provide protection of sql injection attacks[^].
I strongly recommend to use stored procedures and call it from code behind[^]. If you would like to avoid logins duplication, set this field as unique[^]!
How to: Creating and Modifying UNIQUE Constraints[^]
这篇关于重复用户名验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文