新手,用SQL2008做了一个登陆界面,注册,登陆没问题,但是登陆时密码输入次数不增加,求解!
//登录按钮       
 private void btnLogin_Click(object sender, RoutedEventArgs e)
        {                        
                int errortimes = sqlHelper.checkErrorTimes(txtUserName.Text.Trim()); 
                if (errortimes >= 3)
                {
                    MessageBox.Show("输入次数超过三次,用户已锁定!");
                    return;
                }
                if (sqlHelper.check(txtUserName.Text.Trim(), pbPassWord.Password.Trim()))
                {
                    Comsume comWin = new Comsume();
                    comWin.Show();
                    this.Close();
                }
                else
                {
                    sqlHelper.errorTimes(++errortimes, txtUserName.Text.Trim());
//密码输错后感觉数据库中的ErrorTimes不增加!
                    MessageBox.Show("登录失败,用户名或密码不正确!");                    
                }
        }以下为sqlHelper语句(数据库连接语句均已放到配置文件中,测试连接没问题,登陆也没问题,就是密码输错次数不更新)
        //更新用户输入密码错误次数
        public static void errorTimes(int i, string name)
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = string.Format("update T_Users set ErrorTimes={0} where UserName='{1}'", i, name);
                conn.Open();
                conn.Close();
            }
        }
        //检测用户是否输入错误超过三次
        public static int checkErrorTimes(string name)
        {          
            using (SqlCommand cmd = new SqlCommand())
            {
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.SelectCommand.Connection = conn;
                conn.Open();                              
                cmd.CommandText=string.Format("select * from T_Users where UserName='{0}'", name);                            
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                DataTable tb1 =ds.Tables[0];
                DataRow row = tb1.Rows[0];
                conn.Close();
                int result=Convert.ToInt32(row["ErrorTimes"]);
                return result;
            }
        }SQLnetc#登陆密码

解决方案 »

  1.   

    int errortimes 声明为全局的
      

  2.   


     //更新用户输入密码错误次数
            public static void errorTimes(int i, string name)
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = string.Format("update T_Users set ErrorTimes={0} where UserName='{1}'", i, name);
                    conn.Open();
                    conn.Close();
                }
            }cmd都没执行怎么更新数据库?
      

  3.   


    cmd执行了,二楼的是正确的,errortimes没有声明为全局变量,不过还是感谢回答~
      

  4.   

    你更新数据库的时候,SqlCommand也没有设定Connection啊。   
            public static void errorTimes(int i, string name)
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = string.Format("update T_Users set ErrorTimes={0} where UserName='{1}'", i, name);
                    conn.Open();
                    conn.Close();
                }
            }