C# 注册判断用户名是否重复
string sqlselect = "select count(*) from tab where name='"+TextBox1.Text + "'";
string sqlinsert = "insert into tab (name)values('"+TextBox1.Text+"')";
SqlConnection con = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand(sqlselect, con);
con.Open();
if ((int)cmd.ExecuteScalar() > 0)
{
con.Close();
Label1.Text ="用户名已经存在";
}
else
{
SqlCommand cmd1 = new SqlCommand(sqlinsert, con);
cmd1.ExecuteNonQuery();
con.Close();
Label1.Text = "用户名添加成功";
}