这是我的用户注册的代码,在userreg.aspx.cs 中,现在插入记录已经正常了,但我该怎样检测输入的用户名:username 和 数据表 theuser 中字段up_user 中的任何一条不同呢。(唯一性检查)。请高手们帮我插入一下那些代码。protected void Button1_Click(object sender, EventArgs e)
    {
            SqlConnection conn = new SqlConnection();
            ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["upupSQLConnectionString"];
            String connString = s.ConnectionString;
            conn.ConnectionString = s.ConnectionString;
            
            String u_username = this.username.Text.ToString();
            String pass = this.password.Text.ToString();
            String textemail = this.email.Text.ToString();
            
            String sql = string.Format("insert into theuser(up_user,up_pass,up_email) values('{0}','{1}','{2}')", u_username, pass, textemail);
            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql,conn);
            conn.Open();            command.ExecuteNonQuery();
            conn.Close();
            Response.Write("OK");
}

解决方案 »

  1.   

    以username 为条件去查询数据库,看记录返回是否大于1
      

  2.   

    你先select一下看是否有这个记录
      

  3.   

    if exists(select [username] from [table_name] where [username]='user_name')
      

  4.   

    select count(*) from table1 where username='"+username+"'如果返回值大于0则存在记录..否则不存在记录
      

  5.   

    select count(username) from [table_name] where [username]='user_name'
    判断这个值 是不是大于等于1
      

  6.   

    zhoufoxcn(执子之手) ( ) 信誉:100    Blog  2006-12-27 12:48:16  得分: 0  
     
     
       if exists(select [username] from [table_name] where [username]='user_name')
      
     
    这个效率比count高
      

  7.   

    protected void Button1_Click(object sender, EventArgs e)
        {
                SqlConnection conn = new SqlConnection();
                ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["upupSQLConnectionString"];
                String connString = s.ConnectionString;
                conn.ConnectionString = s.ConnectionString;
                
                String u_username = this.username.Text.ToString();
                String pass = this.password.Text.ToString();
                String textemail = this.email.Text.ToString();
                
       String sql = "if exists(select username from tablename where username ='"+user_name+" ')";
       
              sql += string.Format("insert into theuser(up_user,up_pass,up_email) values('{0}','{1}','{2}')", u_username, pass, textemail);
                System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql,conn);
                conn.Open();            command.ExecuteNonQuery();
                conn.Close();
                Response.Write("OK");
    }
      

  8.   

    哪里有问题?  如果存在,则不执行下面的语句,应该怎样写呢?     String sql = "if exists(select up_user from theuser where up_user ='"+username+" ')";
        { 
           Response.Write("用户名已存在");
       Response.Redirect("userreg.aspx");
        }
           else
           {
               sql = string.Format("insert into theuser(up_user,up_pass,up_email) values('{0}','{1}','{2}')", u_username, pass, textemail);
                System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql,conn);
                conn.Open();            command.ExecuteNonQuery();
                conn.Close();
                Response.Write("OK");
    }
       }
      

  9.   

    建唯一键在程序中不好判断是否重复,还是写代码检查较好
    strsql="select count(*) from table1 where username='"+username+"'"
    SqlCommand cmd=new SqlCommand(Session["chartsql"].ToString(),con);
    cmd.CommandType=CommandType.Text;
    SqlDataAdapter da=new SqlDataAdapter();
    da.SelectCommand=cmd;
    DataSet ds=new DataSet();
    da.Fill(ds,"t");
    if(ds.Tables["t"].Rows.Count>0)
    {
    Response.Write("该用户已存在");
    }
    这样就OK了!!
      

  10.   

    这个偶写过. 类似taobao那种,检测到有重复之后在页面给出提示信息!
      

  11.   

    设置一个主键,用出错页面提示用户 try ...    catch ....
      

  12.   

    这里我在数据库里设置usern为主键 确定唯一性  出现重复页面提示
      Dim usern As String = Trim(txt1.Text)
            Dim mima As String = Trim(txt2.Text)
            Dim sconstr As String = _
            System.Configuration.ConfigurationManager.AppSettings("scon")
            Dim con As SqlClient.SqlConnection = _
            New SqlClient.SqlConnection(sconstr)
            Dim sql As String = " insert into test(usern,mima) values(@usern,@mima) "
            Dim cmd As SqlClient.SqlCommand = _
            New SqlClient.SqlCommand(sql, con)
            con.Open()
            Try
                cmd.Parameters.Add(New SqlClient.SqlParameter("@usern", _
        SqlDbType.VarChar, 50)).Value = usern
                cmd.Parameters.Add(New SqlClient.SqlParameter("@mima", SqlDbType.VarChar, _
        50)).Value = mima
                cmd.ExecuteReader()
                lab2.Text = "注册成功"
            Catch ex As Exception
                lab2.Text = "帐号存在重复"
            End Try
            con.Close()
      

  13.   

    给你个SQL操作类 /// <summary> 
    /// 检验是否存在数据 
    /// </summary> 
    /// <returns></returns> 
    public bool ExistDate(string SQL) 

    SqlConnection Conn; 
    Conn = new SqlConnection(ConnStr); 
    Conn.Open(); 
    SqlDataReader Dr ; 
    Dr = CreateCmd(SQL,Conn).ExecuteReader(); 
    if (Dr.Read()) 

    Dispose(Conn); 
    return true; 

    else 

    Dispose(Conn); 
    return false; 

    }
      

  14.   

    现在代码没有出错,但好像不能正确判断。总是说”用户名被占用“ 应该怎样改?
    同志们提供的代码我不知道怎么用进去。就在这上面改吧!谢谢了!
    或者用IF ……ELSE ……行吗? protected void Button1_Click(object sender, EventArgs e)
        {
                SqlConnection conn = new SqlConnection();
                ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["upupSQLConnectionString"];
                String connString = s.ConnectionString;
                conn.ConnectionString = s.ConnectionString;
                
                String u_username = this.username.Text.ToString();
                String pass = this.password.Text.ToString();
                String textemail = this.email.Text.ToString();    
          
           try
           {
               String sql = "if exists(select up_user from theuser where up_user ='" + username + " ')";
              Response.Write("用户名被占用");
           }
           catch 
           {
               String sql = string.Format("insert into theuser(up_user,up_pass,up_email) values('{0}','{1}','{2}')", u_username, pass, textemail);
               System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql, conn);
               conn.Open();
               command.ExecuteNonQuery();
               Response.Write("注册成功"); 
           }
           conn.Close();
       
    }
      

  15.   

    protected void Button1_Click(object sender, EventArgs e)
        {
                SqlConnection conn = new SqlConnection();
                ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["upupSQLConnectionString"];
                String connString = s.ConnectionString;
                conn.ConnectionString = s.ConnectionString;
                
                String u_username = this.username.Text.ToString();
                String pass = this.password.Text.ToString();
                String textemail = this.email.Text.ToString();    
          
           try
           {
               String sql = "if exists(select up_user from theuser where up_user ='" + username + " ') begin select '用户名被占用' end else 
    begin insert into theuser(up_user,up_pass,up_email)
    values ('"+u_username+"', '"+pass+"', '"+textemail+"')
    select '注册成功' 
    end
    "; System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql, conn);
               conn.Open();
               command.ExecuteNonQuery();                 }
           catch 
           {
              
       
               Response.Write("注册失败"); 
           }
           conn.Close();
       
    }
      

  16.   

    protected void Button1_Click(object sender, EventArgs e)
        {
                SqlConnection conn = new SqlConnection();
                ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["upupSQLConnectionString"];
                String connString = s.ConnectionString;
                conn.ConnectionString = s.ConnectionString;
                
                String u_username = this.username.Text.ToString();
                String pass = this.password.Text.ToString();
                String textemail = this.email.Text.ToString();
                
       String sql = "if exists(select username from tablename where username ='"+user_name+" ')";
       
              sql += string.Format("insert into theuser(up_user,up_pass,up_email) values('{0}','{1}','{2}')", u_username, pass, textemail);
                System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql,conn);
                conn.Open();            command.ExecuteNonQuery();
                conn.Close();
                Response.Write("OK");
    }