日期:2014-05-18 浏览次数:20858 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace 登陆练习 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void incErrorTimes() { using (MySqlConnection conn = new MySqlConnection("Server=localhost;User Id=root;Password=xman;Persist Security Info=True;Database=test")) { conn.Open(); using (MySqlCommand updataCmd = conn.CreateCommand()) { updataCmd.CommandText = "update user set errorTimes= errorTimes+1 where userName=@u"; updataCmd.Parameters.Add(new MySqlParameter("u", userText.Text)); updataCmd.ExecuteNonQuery(); } } } private void reSetErrorTimes() { using (MySqlConnection conn = new MySqlConnection("Server=localhost;User Id=root;Password=xman;Persist Security Info=True;Database=test")) { conn.Open(); using (MySqlCommand updataCmd = conn.CreateCommand()) { updataCmd.CommandText = "update user set errorTimes= 0 where userName=@u"; updataCmd.Parameters.Add(new MySqlParameter("u", userText.Text)); updataCmd.ExecuteNonQuery(); } } } private void button1_Click(object sender, EventArgs e) { using (MySqlConnection conn = new MySqlConnection("Server=localhost;User Id=root;Password=xman;Persist Security Info=True;Database=test")) { conn.Open(); using (MySqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from user where userName=@u "; cmd.Parameters.Add(new MySqlParameter("u", userText.Text)); using (MySqlDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { int errorTimes = reader.GetInt32(reader.GetOrdinal("errorTimes")); if (errorTimes > 3) { MessageBox.Show("登陆错误次数过多,禁止登陆!"); return; } String dbpassword = reader.GetString(reader.GetOrdinal("userPass")); if (dbpassword == PassText.Text) { MessageBox.Show("登陆成功!"); reSetErrorTimes(); } else { incErrorTimes(); MessageBox.Show("登陆失败!"); } } else { MessageBox.Show("用户不存在!"); } } } } } } }