using(SqlDataReader reader=cmd.ExecuteReader())
 {
       if(reader.Read())
        {
           //ErrorTimes为数据库表中列名,提示下面一行未处理异常
           int errorTimes = reader.GetInt32(reader.GetOrdinal("ErrorTimes"));
           if(errorTimes>3)
           {
             MessageBox.Show("登录错误次数过多,禁止登录!");
             return;
           }
        }
}
求大神指点,怎么修改!

解决方案 »

  1.   

    你先确保 ErrorTimes是否存在
      

  2.   

    数据库中是存在啊 int型的
      

  3.   

    保证你的查询语句包含该列 
    int errorTimes =(int)reader["ErrorTimes"];//试试
      

  4.   

    你断点看一下,reader["ErrorTimes"]是神马不就清楚了吗?
      

  5.   

    数据库中只有一条记录其中ErrorTimes=0;
    断点 变量ErrorTimes值也是0
      

  6.   

    断点后 运行ErrorTimes就是数据库中的值啊
      

  7.   

     using(SqlConnection conn=new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDBFilename=|DataDirectory|\\MyDB.mdf;Integrated Security=True;User Instance=True"))
                {
                    conn.Open();
                    using(SqlCommand cmd=conn.CreateCommand())
                    {
                        cmd.CommandText="select count(*) from T_Users where UserName=@UserName";
                        cmd.Parameters.Add(new SqlParameter("UserName",textBox1.Text));
                        using(SqlDataReader reader=cmd.ExecuteReader())
                        {
                            while(reader.Read())
                            {
                                int errorTimes = reader.GetInt32(reader.GetOrdinal("ErrorTimes"));
                                if(errorTimes>3)
                                {
                                    MessageBox.Show("登录错误次数过多,禁止登录!");
                                    return;
                                }
                                string dbpassword=reader.GetString(reader.GetOrdinal("Password"));
                                if(dbpassword==textBox2.Text)
                                {
                                    MessageBox.Show("登录成功!");
                                    ResetErrorTimes();//重置登录错误次数
                                     }
                                else
                                {  
                                    IncErrorTimes();
                                    MessageBox.Show("登录失败!");
                                }
                            }                    }
                    }
                }
      

  8.   

    select count(*) from T_Users where UserName=@UserName
    你的SQL语句查询出来就一列,这条SQL语句中没有“ErrorTimes”列,也没有“Password”列啊,在reader里面取值肯定报错。