protected void Page_Load(object sender, EventArgs e)
    {
            Random ro = new Random(); 
            if (!IsPostBack) 
             { 
    
                  this.Label1.Text = ro.Next(1000, 9999).ToString(); 
             } 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {        if (this .TextBox2 .Text !="")//判断用户名是否未空 
             { 
                 if (this .TextBox3 .Text !="")//判断密码是否未空 
                  { 
                      if (this .TextBox1 .Text !="")//判断验证码是否未空 
                        { 
                          if (this .TextBox1 .Text  == this.Label1.Text)//判断验证码是否相等 
                            { 
                              string sql; 
                               sql = "select count(*) from user where username='" + this.TextBox2.Text  + "' and password='" + this.TextBox3.Text  + "'";//建立sql查询语句 
                              
                              OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("./app_data/db2.mdb"));//建立数据库连接 
                             conn.Open(); 
                             OleDbCommand cmd = new OleDbCommand(sql, conn);
                             int state =Convert.ToInt32( cmd.ExecuteScalar());//执行sql语句,并返回获得值 
                            int state =Convert.ToInt32( cmd.ExecuteScalar());   //执行sql语句,并返回获得值 
                             if (state == 0 || state > 1)//
如果数据中没有记录或有多条记录则抱错 
                             { 
                              this.Label4.Text = "用户不存在,请检测用户名和密码是否正确!"; 
                              } 
                             else 
                               { 
                                   this.Label4.Text = "登入成功!" ; 
                                    Response.Redirect ("index.aspx");
                                } 
                          conn.Close(); 
                                                     
                          } 
                    else 
                       { 
                  this.Label4.Text = "验证码不正确,请重新输入!"; 
                        } 
                        } 
                        else 
                         { 
                        this.Label4.Text = "验证码没有填写!";                          } 
                         } 
                     else 
                        { 
                     this.Label4.Text = "密码没有填写!";                         } 
                        } 
                    else 
                       { 
                          this.Label4.Text = "用户名没有填写!"; 
                        } 
                        } 

解决方案 »

  1.   


     sql = "select count(*) as num from [user] where [username]='" + this.TextBox2.Text  + "' and password='" + this.TextBox3.Text  + "'";//建立sql查询语句 
      

  2.   

     int state =Convert.ToInt32( cmd.ExecuteScalar().ToString());  //执行sql语句,并返回获得值
    cmd.ExecuteScalar()返回的Object类型的值,而Convert.ToInt32(string v)需要String类型的参数,调用ToString()方法就能满足条件了。
      

  3.   

    sql = "select count(*) from user where [username]='" + this.TextBox2.Text  + "' and [password]='" + this.TextBox3.Text  + "'";//建立sql查询语句
    UserName不记得了,password好像是Access的关键字。