小白学语句:SQL笔记4

练习4:
做一个登录小程序,登录错误3次之后禁止登录
首先在Program.cs加入那段代码

string dataDir = AppDomain.CurrentDomain.BaseDirectory;
            if (dataDir.EndsWith(@"\bin\Debug\")
                || dataDir.EndsWith(@"\bin\Release\"))
            {
                dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
                AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
            }

这里写图片描述
然后再回到按扭的编程里,看到两个类InErrorTimes()错误次数+1和ResetErrorTimes()//重置错误次数
这是因为在同一个连接中,如果SqlDataReader没有关闭,那么是不能执行Update之类的语句

private void InErrorTimes()//错误次数+1
        {
            using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C#\TheFirstmdf\登录测试1\MyDB.mdf;Integrated Security=True"))
            {
                conn.Open();
                using (SqlCommand updatecmd = conn.CreateCommand())
                {
                    updatecmd.CommandText = "update T_UserText1 set ErrorTimes=ErrorTimes+1 where UserName=@UserName";
                    updatecmd.Parameters.Add(new SqlParameter("UserName", txtUserName.Text));
                    updatecmd.ExecuteNonQuery();
                }
            }
        }
        private void ResetErrorTimes()//重置错误次数
        {
            using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C#\TheFirstmdf\登录测试1\MyDB.mdf;Integrated Security=True"))
            {
                conn.Open();
                using (SqlCommand updatecmd = conn.CreateCommand())
                {
                    updatecmd.CommandText = "update T_UserText1 set ErrorTimes=0 where UserName=@UserName";
                    updatecmd.Parameters.Add(new SqlParameter("UserName", txtUserName.Text));
                    updatecmd.ExecuteNonQuery();
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C#\TheFirstmdf\登录测试1\MyDB.mdf;Integrated Security=True"))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from T_UserText1 where UserName=@UserName";
                    cmd.Parameters.Add(new SqlParameter("UserName", txtUserName.Text));
                    using (SqlDataReader 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("Password"));
                            if(dbpassword ==txtPassword .Text )
                            {
                                MessageBox.Show("登录成功!");
                                ResetErrorTimes();
                            }
                            else
                            {
                               InErrorTimes();
                                MessageBox.Show("登录失败!");
                            }
                        }
                        else
                        {
                            MessageBox.Show("用户名不存在!");
                        }
                    }
                }
            }
        }

利用using SqlConnection建立连接
using (SqlConnection conn = new SqlConnection(@”Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C#\TheFirstmdf\登录测试1\MyDB.mdf;Integrated Security=True”))
用CommandText获取输入的内容在数据库里查到相应的数据。
利润using SqlDataReader返回的结果作判断
如果reader返回true那就有数据,返回false就是没有数据
if(reader .Read ())
{ }
else
{MessageBox.Show(“用户名不存在!”);}
返回值是true的时候,接着判断密码是不是跟数据库中记录的一样,一样就登录成功,不一样就错识次数+1
string dbpassword = reader.GetString(reader.GetOrdinal(“Password”));//获取数据库中对应的password
if(dbpassword ==txtPassword .Text )
{
MessageBox.Show(“登录成功!”);
ResetErrorTimes();//错误次数清0
}
else
{
InErrorTimes();
MessageBox.Show(“登录失败!”);
}
如果错误次数不清0,那密码错误的时候就会被累积,一直到超过三次之后。所以正常的是登录成功之后错误次数清0.
最后加上错误次数是否已经大于3次,如果是就禁止登录,直接return出来
int errorTimes = reader.GetInt32(reader.GetOrdinal(“ErrorTimes”));
if(errorTimes >3)
{
MessageBox.Show(“登录错误次数过多,禁止登录!”);
return;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值