ADO.NET复习总结(5)--工具类SqlHelper 实现登录
工具类SqlHelper
即:完成常用数据库操作的代码封装
一、基础知识
1、每次进行操作时,不变的代码:
(1)连接字符串;
(2)往集合存值;
(3)创建连接对象、命令对象;
(4)打开连接;
(5)执行命令
2、每次操作时,变化的代码:
(1)sql语句;
(2)参数

3、配置文件(关于配置这篇文章讲的挺详细的:https://www.cnblogs.com/programsky/p/4592141.html)
好处:修改方便;
维护成本降低,修改程序不需要重新编译。


代码为:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="dbtest" connectionString="server=.;database=dbtest;uid=sa;pwd=123"/>
</connectionStrings>
</configuration>
二、开始封装
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace t1_UserLogin
{
public static partial class SqlHelper
{
private static string connStr = ConfigurationManager.ConnectionStrings["dbtest"].ConnectionString; //执行查询:select返回多行多列
public static SqlDataReader ExecuteReader (string sql, params SqlParameter[] ps)//SqlParameter[] ps=new SqlParameter[];
{
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(sql, conn);
if (ps.Length > )
{
cmd.Parameters.AddRange(ps);
} conn.Open();
//使用SqlDataReader时,连接必须是打开的;设置此参数后,关闭SqlDataReader时会自动关闭使用的连接
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
} //执行查询:select返回首行首列
public static object ExecuteScalar(string sql, params SqlParameter[] ps)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddRange(ps); conn.Open();
return cmd.ExecuteScalar();
}
}
//执行操作:insert,update,delete
public static int ExecuteNonQuery(string sql, params SqlParameter[] ps)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddRange(ps); conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
}
三、实现登录
用户连接三次登录失败,则锁定15分钟,15分钟之后才可以再使用
实现简单登录
MD5加密:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; namespace t1_UserLogin
{
public static partial class Md5Helper
{
public static string Encrypt(string pwd)
{
MD5 md5 = MD5.Create(); //将字符串转换成字符数据:指定编码
byte[] pwd2 = Encoding.UTF8.GetBytes(pwd); byte[] pwd3 = md5.ComputeHash(pwd2); StringBuilder sb=new StringBuilder("");
for (int i = ; i < pwd3.Length; i++)
{
sb.Append(pwd3[i].ToString("x2").ToLower());
}
//0-255
//00-ff 10=>16 07
return sb.ToString();
}
}
}
1、登录代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using t1_UserLogin; namespace login
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnLogin_Click(object sender, EventArgs e)
{
string sql = "select userpwd from userinfo where username=@name";
SqlParameter p = new SqlParameter("@name", txtName.Text);
object pwd = SqlHelper.ExecuteScalar(sql, p);
if(pwd==null)
{
MessageBox.Show("用户名错误");
}
else if (pwd.ToString().Equals(Md5Helper.Encrypt(txtbwd.Text)))
{
MessageBox.Show("登录成功");
}
else
{
MessageBox.Show("密码错误");
}
}
}
}
2、登录代码:(锁定15分钟)
(1)登录逻辑(重要)

(2)数据库

(3)初级代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using t1_UserLogin; namespace login
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnLogin_Click(object sender, EventArgs e)
{
#region 锁定15分钟 string sql = "select count(*) from userinfo where username=@name";
SqlParameter p = new SqlParameter("@name", txtName.Text); int count = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, p));
if (count > )
{
sql =
"select count(*) from userinfo where username=@name and errorcount>=3 and datediff(Minute,errortime,getdate())<=15";
SqlParameter p11 = new SqlParameter("@name", txtName.Text);
count = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, p11));
if (count > )
{
MessageBox.Show("账户已被锁定");
}
else
{
//当前未被锁定
sql = "select count(*) from userinfo where username=@name and userpwd=@pwd";
SqlParameter p12 = new SqlParameter("@name", txtName.Text);
SqlParameter p2 = new SqlParameter("@pwd", Md5Helper.Encrypt(txtbwd.Text));
count = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, p12, p2));
if (count > )
{
sql = "update userinfo set errorcount=0 where username=@name";
SqlParameter p13 = new SqlParameter("@name", txtName.Text);
SqlHelper.ExecuteNonQuery(sql, p13);
MessageBox.Show("成功");
}
else
{
//出错,更新次数与时间
sql = "update userinfo set errorcount=errorcount+1,errortime=getdate() where username=@name";
SqlParameter p14 = new SqlParameter("@name", txtName.Text);
SqlHelper.ExecuteNonQuery(sql, p14);
MessageBox.Show("密码错误");
}
}
}
else
{
MessageBox.Show("用户不存在");
} #endregion }
}
}
3、登录代码:(锁定15分钟)-——————优化
(1)逻辑*(重要)

(2)代码优化
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using t1_UserLogin; namespace login
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnLogin_Click(object sender, EventArgs e)
{
#region 锁定15分钟-优化 string sql = "select errorcount,errortime,userpwd from userinfo where username=@name";
SqlParameter p = new SqlParameter("@name", txtName.Text); using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, p))
{
if (reader.Read())
{
//当前用户名存在
int errorCount = Convert.ToInt32(reader["errorCount"]);
double errorTime1 = ;
//如果单元格返回空值,使用DBNull.Value进行判断
if (reader["ErrorTime"] != DBNull.Value)
{
DateTime errorTime = Convert.ToDateTime(reader["errorTime"]);
errorTime1 = (DateTime.Now - errorTime).TotalMinutes;
}
string pwd1 = reader["userPwd"].ToString();
string pwd2 = Md5Helper.Encrypt(txtbwd.Text);
if (errorCount >= )
{
//超过3次
if (errorTime1 <= )
{
//过时
MessageBox.Show("锁定");
}
else
{
int count1 = ;
if (pwd1.Equals(pwd2))
{
count1 = ;
MessageBox.Show("成功");
}
else
{
count1 = ;
MessageBox.Show("密码错误");
}
//字符串拼接
sql = "update userinfo set errorCount=" + count1 + ",errortime=getdate() where username=@name";
p = new SqlParameter("@name", txtName.Text);
SqlHelper.ExecuteNonQuery(sql, p);
}
}
else
{
//不足三次
if (errorTime1 <= )
{
int count1 = ;
if (pwd1.Equals(pwd2))
{
count1 = ;
MessageBox.Show("成功");
}
else
{
count1 = errorCount + ;
MessageBox.Show("密码错误");
}
sql = "update userinfo set errorCount=" + count1 + ",errortime=getdate() where username=@name";
p = new SqlParameter("@name", txtName.Text);
SqlHelper.ExecuteNonQuery(sql, p); }
else
{
int count1 = ;
if (pwd1.Equals(pwd2))
{
count1 = ;
MessageBox.Show("成功");
}
else
{
count1 = ;
MessageBox.Show("密码错误");
}
sql = "update userinfo set errorcount=" + count1 + ",errortime=getdate() where username=@name";
p = new SqlParameter("@name", txtName.Text);
SqlHelper.ExecuteNonQuery(sql, p);
}
}
}
else
{
MessageBox.Show("用户名不存在");
}
} #endregion }
}
}
ADO.NET复习总结(5)--工具类SqlHelper 实现登录的更多相关文章
- SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 后端篇(五): 数据表设计、使用 jwt、redis、sms 工具类完善注册登录逻辑
(1) 相关博文地址: SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 前端篇(一):搭建基本环境:https://www.cnblogs.com/l-y-h/p ...
- 制作ado开发辅助工具类SqlHelper
public static class SqlHelper { //通过配置文件获取连接字符创 private static readonly string constr = Configuratio ...
- vs工具类SQLhelper参考
参考 https://www.cnblogs.com/liyangLife/p/5036636.html
- 利用JDBC工具类 模拟用户登录!
一.建库 设置 id为主键并自增! 二.定义登录接口 package com.aaa.dao; public interface IDengDao { /* 1.定义一个登陆的接口,参数是name 和 ...
- PHP文件上传,下载,Sql工具类!
PHP文件上传,下载,Sql工具类! 对文件大小,文件类型 同名覆盖 中文转码的操作,可直接使用 前台 upload.html <!DOCTYPE html> <html> & ...
- 4 多表代替密码之Hill 密码_1 矩阵工具类
在说明Hill加密之前要先复习线性代数的知识,主要是关于矩阵的一些运算和概念. 一.矩阵的逆: 定义方阵M的逆矩阵应该满足M*M^-1==I,其中I是单位矩阵,比如: 但是这个地方是对英文字母进行加密 ...
- 基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil
基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil,把日常能用到的各种CRUD都进行了简化封装,让普通程序员只需关注业务即可,因为非常简单,故直接贴源代码,大家若需使用可以直 ...
- 【转载】微软官方提供的Sqlserver数据库操作帮助类SQLHelper类
在.NET平台中,C#语言一般使用ADO.NET组件来操作Sqlserver数据库,通过ADO.NET组件可以实现连接数据库.查询数据集.执行SQL语句以及关闭数据库连接等操作,为此网上有很多开发者自 ...
- C#工具类:使用SharpZipLib进行压缩、解压文件
SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...
随机推荐
- ABP .Net Core 日志组件集成使用NLog
一.说明 NLog介绍和使用说明官网:http://nlog-project.org/ NLog和Log4net对比:https://www.cnblogs.com/qinjin/p/5134982. ...
- angular4.0如何引入外部插件2:declare方案
前面有个<angular4.0如何引入外部插件1:import方案>,但是有局限,因为方案1需要用到@types这个东西. 但是并不是每一个插件都有@types,所以现在写个方案2. 拿引 ...
- 使用腾讯云无服务器云函数(SCF)分析天气数据
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:李想 无服务器云函数(SCF)是腾讯云提供的Serverless执行环境,也是国内首款FaaS(Function as a Service ...
- C++\virtual 虚函数、纯虚函数
前提摘要: 虚函数联系到多态,多态联系到继承.所以本文中都是在继承层次上做文章.没了继承,什么都没得谈. 虚函数定义: 指向基类的指针或引用在操作它的多态类(子类/派生类)对象时,会根据不同的类对象, ...
- 安装adb之后出现 找不到设备的情况
adb 地址 https://pan.baidu.com/s/1sln2IZF 安装adb之后出现 找不到设备的情况 1.配置adb的环境变量 2.修改android_winusb.inf ...
- Linux经常使用的文件传输的几种方式
Linux经常使用的文件传输的几种方式 1.终端新建stfp协议连接 或者命令方式: sftp -P22 root@192.168.11.100 端口可以不用填写,默认是22,端口的P是大写. 将本地 ...
- Python 集合 深浅copy
一,集合. 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. ...
- Python模块学习系列
python模块-time python模块-datetime python模块-OS模块详解
- spring boot +mysql + mybatis + druid的整理(一)——单数据源
一,使用spring boot脚手架搭建spring boot框架生成maven项目 如下图所示: 设置自定义的坐标,即左侧的Group和Artifact,右侧可以搜索添加一些依赖,搜索不到的可以在p ...
- Eclipse中使用Maven新建 Servlet 2.5的 SpringMVC项目
1.前言: 最近在学习SpringMVC框架,由于使用Eclipse创建的webAPP项目默认使用的还是比较旧的servlet2.3,而且默认使用的还是JDK1.5,所以便有一次开始了我的配置之路 2 ...