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 = "用户名添加成功";
            }

posted on 2014-02-15 15:37  小马哥~程序之美  阅读(1997)  评论(0编辑  收藏  举报

导航