我现在在写一个注册窗口,是连接到sql数据库的,请问在注册的时候要如何检测输入的用户名是否已经被启用?

解决方案 »

  1.   


    protected void Insert_Click(object sender, EventArgs e)
        {
            if ((StudentID.Text.Trim()).Length < 1)
            {
                Response.Write("<script>window.alert('新插入项ID不能为空!')</script>");
                return;
            }
            int total = 0;
            SqlConnection con = new SqlConnection("data source=.;user id=sa;pwd=;database=Student");
            con.Open();
            string str = "select count(*) as Total from Student where sno=" + StudentID.Text.Trim();
            SqlCommand cmd = new SqlCommand(str, con);
            SqlDataReader sr = cmd.ExecuteReader();
            if (sr.Read())
            {
                total = int.Parse(sr["Total"].ToString());
                if (total > 0)
                    Response.Write("<script>window.alert('数据库中有该记录的记录!请重新输入!')</script>");
            }
            sr.Close();
            con.Close();
            if (total == 0)
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
                string insert = "insert into Student(sno,sname,sage,sdept) values(" + StudentID.Text.Trim() + "," + "'" + StudentName.Text.Trim() + "'" + "," +
                                 StudentAge.Text.Trim() + "," + "'" + StudentDept.Text.Trim() + "'" + ")";
                Response.Write(insert);
                SqlCommand cmd1 = new SqlCommand(insert, con);
                cmd1.ExecuteNonQuery();
                con.Close();
            }    }
    我之前做的例子,给LZ提供思路
      

  2.   

    private void btnSent_Click(object sender, EventArgs e)
            {
                MessageBox.Show(this, "确认无误?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                //建立连接
                SqlConnection mysql = new SqlConnection("Data Source=.;Initial Catalog=elevtive;Persist Security Info=True;User ID=sa;Password=zt201314");
                //开启数据库连接
                mysql.Open();
                //定义语句(发送语句)
                string select = "select * from tb_student";
                SqlCommand mycmd = new SqlCommand(select, mysql);
                //定义数据库阅读器(执行命令)
                SqlDataReader reader = mycmd.ExecuteReader();
                int i = 0;
                //检测账户是否已经被使用
                while (reader.Read())
                {
                    if (txtID.Text == reader["stuID"].ToString())
                    {
                        MessageBox.Show("账户已存在");
                        i = 1;
                    }
                    break;
                }
                mysql.Close();
                //账户未被使用
                if (i != 1)
                {
                    //密码前后一致
                    if (txtPassword.Text == txtPassword2.Text)
                    {
                        mysql.Open();
                        string register = "insert into tb_student( stuID,stuName,stuSex,stuBirthday,stuClass,stuPassword) values ('" + txtID.Text + "','" + txtName.Text + "','" + txtSex.Text + "','" + txtBirthday.Text + "','" + txtClass.Text + "','" + txtPassword.Text + "')";
                        SqlCommand reg = new SqlCommand(register, mysql);
                        reg.ExecuteNonQuery();//返回影响的行数(显示删除,更新是否成功,可以+‘>0’)
                        MessageBox.Show("注册成功");
                        mysql.Close();
                        frmLogin login = new frmLogin();
                        login.Show();
                        this.Hide();                }
                    //前后密码不一致
                    else
                        MessageBox.Show(this,"密码输入不一致,请重新输入","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
            }        private void btnExit_Click(object sender, EventArgs e)
            {
                
                    frmLogin login = new frmLogin();
                    login.Show();
                    this.Hide();我的程序是这样的写的。对于新的账号我能注册,不过我试一下已注册的账号,那个检测直接被跳过了没有提示“账户已存在”。。
    还有我是新手,请高手帮我优化优化。
      

  3.   


     string select = "select * from tb_student";
      SqlCommand mycmd = new SqlCommand(select, mysql);这条SQL语句应该添加条件
    改成
    string select = "select * from tb_student where 账户ID = '"+stuID.Text.Trim()+"'";没有条件判断就会跳过
      

  4.   

    应该是
    string select = "select * from tb_student where stuID= '"+txtID.Text.Trim()+"'";
     
      

  5.   


    我用完之后,还是没有显示已存在,reg.ExecuteNonQuery();这一句还是出现了说:“违反了primary key 约束 ‘pk_tb_student_AEC98FAF1108B795B’,不能在对象‘dbo.tb_student’中插入重复键
      

  6.   


    我是用char类型,限制长度为12
      

  7.   


    Textbox的TextChanged事件。。根据输入值在数据库里查一下。。select count(*) from 表名 where 用户名 = 输入的值。。如查返回>0。。表示存在提示己存在
      

  8.   

    你的主键 是 哪个字段
    建议你不要用用户名做主键
    应增加1个字段为ID 的自动增加的 为主键这样你增加记录的时候就不会出现 插入重复键的问题了,我觉得你的问题应该是有两个:
    1、 你的主键 不是你SQL 中的字段, 你又没有哦定义它为自动增长,这样你的插入语句,每次插入都会出现 你的问题
    2、你的SQL 查询 用户名是否存在时,查询的结果判断不对, 
    当你 
     string select = "select * from tb_student";
    SqlCommand mycmd = new SqlCommand(select, mysql);
    修改string select = "select * from tb_student where stuID= '"+txtID.Text.Trim()+"'";
    SqlCommand mycmd = new SqlCommand(select, mysql);把 while (reader.Read())
    修改为 if(reader.Read())
    {
    tishi
    }