using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace 登录程序
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }  private void IncErrortimes()
  {
  using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;
  AttachDBFilename=|DataDirectory|\myDB.mdf;
  Integrated Security=True;User Instance=True"))  using (SqlCommand updateCmd = con.CreateCommand())
  {
  con.Open();
  updateCmd.CommandText = "update Mytable set Errortimes=Errortimes+1 where UserName=@UN";
  updateCmd.Parameters.Add(new SqlParameter("UN", Usernametextbox.Text));
    
  updateCmd.ExecuteNonQuery();
  }
  MessageBox.Show("登录失败!!!");  }
    
  private void button1_Click(object sender, EventArgs e)
  {
  using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;
AttachDBFilename=|DataDirectory|\myDB.mdf;
Integrated Security=True;User Instance=True"))
  {
  con.Open();
  using (SqlCommand cmd = new SqlCommand())
  {
  cmd.Connection = con;
  cmd.CommandText = "select * from Mytable where Username=@UN";
  cmd.Parameters.Add(new SqlParameter("UN", Usernametextbox.Text));
  Username=Usernametextbox.Text";
  using (SqlDataReader reader = cmd.ExecuteReader())
  {
  if (reader.Read())
  {
  int arrortime = reader.GetInt32(reader.GetOrdinal("Errortimes"));
  if (arrortime>2)  {
  MessageBox.Show("登录次数过多,禁止登录!!!");
  ResetErrortimes();
  return;
  }
  string dbpassword = reader.GetString(reader.GetOrdinal("Password"));
  if (dbpassword==PasswordtextBox.Text)
  {
  MessageBox.Show("登录成功!");
  }
  else
  {
  IncErrortimes();
  }
  }
  else
  {
  MessageBox.Show("用户名不存在");
  }
  }
  }
  }  }  private void button2_Click(object sender, EventArgs e)
  {
  creatTable();
  MessageBox.Show("创建新表成功!!!");
  }
  }
}
当某一用户名连续登录三次失败会报错"登录次数过多,禁止登录!!!";(可见Mytable表字段Errortimes数据变成3了)
但是打开表后该对应用户Errortimes字段值还是0