ADO.NET学习笔记(三)

来源:互联网 发布:c语言 long 编辑:程序博客网 时间:2024/05/22 11:36
(1)登陆窗体的登陆过程验证:using System;using System.Windows.Forms;using System.Data.SqlClient;namespace 登陆窗体{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void IntErrorTime()        {            using (SqlConnection conn = new SqlConnection(@"Data Source = .\SQLEXPRESS;AttachDBFilename = |DataDirectory|\MyDB.mdf;Integrated Security = True;  User Instance =True"))            {                conn.Open();                using (SqlCommand updateCmd = conn.CreateCommand())                {  //在同一个连接中,如果SqlDateReader没有关闭,那么就不能执行Update                    updateCmd.CommandText = "update T_User set ErrorTimes=ErrorTimes+1 where UserName = @UN";                    updateCmd.Parameters.Add("UN", txtUserName.Text);                    updateCmd.ExecuteNonQuery();                }            }        }        private void RecentErrorTime()        {            using (SqlConnection conn = new SqlConnection(@"Data Source = .\SQLEXPRESS;AttachDBFilename = |DataDirectory|\MyDB.mdf;Integrated Security = True;  User Instance =True"))            {                conn.Open();                using (SqlCommand updateCmd = conn.CreateCommand())                {  //在同一个连接中,如果SqlDateReader没有关闭,那么就不能执行Update                    updateCmd.CommandText = "update T_User set ErrorTimes=0 where UserName = @UN";                    updateCmd.Parameters.Add("UN", txtUserName.Text);                    updateCmd.ExecuteNonQuery();                }            }        }        private void button1_Click(object sender, EventArgs e)        {            using (SqlConnection conn = new SqlConnection(@"Data Source = .\SQLEXPRESS;AttachDBFilename = |DataDirectory|\MyDB.mdf;Integrated Security = True;  User Instance =True"))//实现了IDisposable接口,用using括起,便于自动释放,在using()后调用了IDisposabl方法,它先判断有无conn.Close();如果没有,先进行关闭,在释放            {                conn.Open();                using (SqlCommand cmd = conn.CreateCommand())//创建命令对象的实例并与先建的数据库建立连接,将连接using进来,使此方法直接不用释放,出了括号会自动释放                {                    cmd.CommandText = "select * from T_User where UserName=@UN";                    cmd.Parameters.Add("UN", txtUserName.Text);                    using (SqlDataReader reader = cmd.ExecuteReader())                    {                        if (reader.Read())                        {                            int errorTimes = reader.GetInt32(reader.GetOrdinal("ErrorTimes"));                            if (3 < errorTimes)                            {                                MessageBox.Show("登陆次数过多,禁止登陆!");                                return;                            }                            else                            {                                string dbpassword = reader.GetString(reader.GetOrdinal("Password"));                                if (txtPassword.Text == dbpassword)                                {                                    MessageBox.Show("登陆成功!");                                    RecentErrorTime();                                }                                else                                {                                    IntErrorTime();                                    //using (SqlCommand updatecmd = conn.CreateCommand())                                    //{  //在同一个连接中,如果SqlDateReader没有关闭,那么就不能执行Update                                    //    updatecmd.CommandText = "update T_User set ErrorTimes=ErrorTimes+1 where UserName = @UN";                                    //    updatecmd.Parameters.Add("UN", txtUserName.Text);                                    //    updatecmd.ExecuteNonQuery();                                    //}                                    MessageBox.Show("登陆失败!");                                }                            }                        }                        else                        {                            MessageBox.Show("用户名不存在~");                        }                    }                }            }        }    }}