C# 判断用户是否存在

304 阅读1分钟
protected void lnbTest_Click(object sender, EventArgs e)
    {
        //连接数据库
        string conStr="server=localhost;Integrated Security=SSPI;database=qyzx";
        SqlConnection conn = new SqlConnection(conStr);
        conn.Open();
        string UserName = this.txtUserName.Text;
        //创建SQL语句
        string selStr = "select * from Users where UserName='"+UserName+"'";
        //创建数据适配器
        SqlDataAdapter da = new SqlDataAdapter(selStr,conn);
        //创建满足条件的数据集
        DataSet ds = new DataSet();
        da.Fill(ds);
        //如果数据集不为空,则用户名已经存在
        if (ds.Tables[0].Rows.Count != 0)
        {
            Response.Write("<script>alert('该用户名已经存在!')</script>");
        }
        else
        {
            Response.Write("<script>alert('该用户名可以注册!')</script>");
        }
    }