工具类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 实现登录的更多相关文章

  1. SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 后端篇(五): 数据表设计、使用 jwt、redis、sms 工具类完善注册登录逻辑

    (1) 相关博文地址: SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 前端篇(一):搭建基本环境:https://www.cnblogs.com/l-y-h/p ...

  2. 制作ado开发辅助工具类SqlHelper

    public static class SqlHelper { //通过配置文件获取连接字符创 private static readonly string constr = Configuratio ...

  3. vs工具类SQLhelper参考

    参考 https://www.cnblogs.com/liyangLife/p/5036636.html

  4. 利用JDBC工具类 模拟用户登录!

    一.建库 设置 id为主键并自增! 二.定义登录接口 package com.aaa.dao; public interface IDengDao { /* 1.定义一个登陆的接口,参数是name 和 ...

  5. PHP文件上传,下载,Sql工具类!

    PHP文件上传,下载,Sql工具类! 对文件大小,文件类型 同名覆盖 中文转码的操作,可直接使用 前台 upload.html <!DOCTYPE html> <html> & ...

  6. 4 多表代替密码之Hill 密码_1 矩阵工具类

    在说明Hill加密之前要先复习线性代数的知识,主要是关于矩阵的一些运算和概念. 一.矩阵的逆: 定义方阵M的逆矩阵应该满足M*M^-1==I,其中I是单位矩阵,比如: 但是这个地方是对英文字母进行加密 ...

  7. 基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil

    基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil,把日常能用到的各种CRUD都进行了简化封装,让普通程序员只需关注业务即可,因为非常简单,故直接贴源代码,大家若需使用可以直 ...

  8. 【转载】微软官方提供的Sqlserver数据库操作帮助类SQLHelper类

    在.NET平台中,C#语言一般使用ADO.NET组件来操作Sqlserver数据库,通过ADO.NET组件可以实现连接数据库.查询数据集.执行SQL语句以及关闭数据库连接等操作,为此网上有很多开发者自 ...

  9. C#工具类:使用SharpZipLib进行压缩、解压文件

    SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...

随机推荐

  1. Django学习日记05_模板_模板语言

    Variables 在模板中,使用两个大括号包含变量的方式来使用变量: {{ name }} 该变量应该作为键值对中的键,在Context中能被查找到. Tags 模板中使用Tags来进行简单的逻辑: ...

  2. 自己动手写把”锁”---LockSupport介绍

    本篇是<自己动手写把"锁">系列技术铺垫的最后一个知识点.本篇主要讲解LockSupport工具类,它用来实现线程的挂起和唤醒. LockSupport是Java6引入 ...

  3. eclipse环境下日志打印输出

    1.先将jdk配置一下 选Preferences---- 找到自己的jdk所在的位置 2.配置Tomcat window-----preferences------- 找到自己的tomcat所在位置 ...

  4. Linux 常见命令示例【一】

    查看端口占用 [查看目前系统上已在监听的网络联机及其pid netstat –tlnp] 文件挂载 Linux与windows文件传输(三方软件:secureCRT, WINscp) 1)sftp S ...

  5. SQLAlchemy基础操作二

    多线程示例 import time import threading from sqlalchemy.ext.declarative import declarative_base from sqla ...

  6. windows系统安装securtCRT

    说明:securtCRT可以ssh liunx主机,或者网络设备,如路由器,交换机,防火墙等设备,很多新手不会安装,因为正版要钱啊,对于小老百姓,还是用破解的吧 不说废话,开始搞起来. 软件下载链接: ...

  7. 分布式文件系统FastDFS如何做到高可用

    FastDFS是用C语言编写的一款开源的轻量级分布式文件系统.它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的 ...

  8. 用JAVA写一个冒泡排序

    一:实现思想: 基本思想:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒.即:每当两相邻的数比较后发现它们的排序与排序要 ...

  9. 一个大四毕业生想对自学Android的大学生说一些话

    本人大四,经历了秋招和春招,秋招拿了华为的android offer,春招是拿的java后台开发的offer,一路走来,感慨很多,有一些话想对在自学Android的大学生说.本文只是帮助像我一样的大学 ...

  10. C#学习笔记-状态模式

    题目1:通过代码描述每一天的不同时间段的工作效率 分析: 首先确定,工作状态指正在工作,或者以及下班这些情况,而这些情况所受影响的因素包括:当前时间以及任务是否已经完成.所以在Work中需要两个属性: ...