• ADO之密码验证--3次错误就锁定


    这个程序是那vs2010下写的,C#语言。数据库是sql server 2008

    首先在数据库中新建一个数据库Test1,在数据库中新建一个表用来保存用户名和密码USERINFO,

    1 CREATE TABLE USERINFO
    2 (
    3     UserID        varchar(12)        NOT NULL  PRIMARY KEY,
    4     Password    varchar(12)        NOT NULL ,
    5     ErrorTimes  int             NULL,
    6 )
    ErrorTimes就是用来判断用户输入密码错误的次数的,每错一次加一

    添加几条数据

    1 INSERT INTO USERINFO 
    2     VALUES('song','123456')
    3 INSERT INTO USERINFO 
    4     VALUES('wang','123456')

    然后,在VS里做个简单界面,给控件绑定变量tb_UserID和tb_Password

    最后响应登录按钮btn_Login_Click()

     1   private void btn_Login_Click(object sender, RoutedEventArgs e)
     2         {
     3             //从控件获取数据
     4             string userid = tb_UserID.Text;
     5             string password = tb_Password.Text;
     6             if (userid != "" && password != "")//用户名和密码全不为空
     7             {
     8                 using (SqlConnection conn = new SqlConnection(
     9                      "Data Source=.;Initial Catalog=Test1;User ID=sa;Password=123456"))
    10                 {              
    11                     conn.Open();
    12                     using (SqlCommand cmd = conn.CreateCommand())
    13                     {
    14                         //查找用户名是否存在
    15                         cmd.CommandText = "select ErrorTimes from USERINFO where UserID=@user";
    16                         cmd.Parameters.Add(new SqlParameter("@user", tb_UserID.Text));//
    17                         int resoult = -1;
    18                         try {
    19                                    resoult = (int)cmd.ExecuteScalar();//如果用户名不存在就抛出异常
    20                             }
    21                         catch (Exception ex) {
    22                                if (ex is SqlException)
    23                                { 
    24                                    MessageBox.Show("用户不存在,请重试");
    25                                }
    26                             }
    27                                                
    28                         if (resoult >= 3)//输入密码错误超过3次
    29                         {
    30                             MessageBox.Show("你的帐户已锁定,请找管理员解锁");
    31                         }
    32                         else if (resoult >= 0 && resoult < 3)
    33                         {
    34                             cmd.CommandText = "select Password from USERINFO where UserID=@user";
    35                             string pass = (string)cmd.ExecuteScalar();                     
    36                             if (pass.Equals(password))//比较两个字符串是否相等
    37                             {
    38                                 MessageBox.Show("登录成功");
    39                             }
    40                             else
    41                             {
    42                                 MessageBox.Show("密码错误,请重试");
    43                                 cmd.CommandText = "update USERINFO set ErrorTimes = ErrorTimes+1 where UserID=@user";//更新数据
    44                                 cmd.ExecuteNonQuery();
    45                             }
    46                         }
    47                         else if (resoult == -1)
    48                         {
    49                             MessageBox.Show("用户不存在,请重试");
    50                         }
    51                         //MessageBox.Show(resoult.ToString());
    52                     }
    53                 }
    54             }
    55             else if (userid == "")
    56             {
    57                 MessageBox.Show("用户名不能为空");
    58             }
    59             else 
    60             {
    61                 MessageBox.Show("密码不能为空");
    62             }
    63         }

    这里总结一下数据库连接的一般过程

      第一步,建立连接使用SqlConnection,参数可以为空,也可以为一个字符串。

          DataSource 表示数据库的地址,如果是本机的话可以用"."或者127.0.0.1,其他的就指定一个IP地址。

          Initial Catalog 表示要连接的数据库名,

          User ID 指定用户名,

          Password指定密码

      SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test1;User ID=sa;Password=123456")

      第二步,连接成功后就可以打开数据库里,Open()方法使用 ConnectionString 所指定的属性设置打开数据库连接。

      conn.Open();

      第三步,对 SQL Server 数据库执行的一个 Transact-SQL 语句或存储过程,使用 SqlCommand类来指定要执行的语句。

       SqlCommand cmd = conn.CreateCommand();//创建一个命令用于执行查询

       cmd.CommandText = "select ErrorTimes from USERINFO where UserID=@user";//这个就是要执行的sql语句

         cmd.Parameters.Add(new SqlParameter("@user", tb_UserID.Text));//这个给参数赋值,可以指定多个参数

      最后执行语句  int resoult = (int)cmd.ExecuteScalar();

      ExecuteScalar();//返回结果集的第一行第一列的数据

      ExecuteNonQuery();//执行语句但是不查询

    当然上面那个例子有很多需要改进的地方,继续学习。

  • 相关阅读:
    windows安装psycopg2问题解决方案
    error: Setup script exited with error: Unable to find vcvarsall.bat
    python web server comparison
    重置mysql root密码
    Nginx + uWSGI + web.py 搭建示例
    《凉州曲》——吴践道
    C#代理服务器
    OpenGL 分形 Sierpinski镂垫
    Chap02_递归与分治_判断两棵二叉树是否同构
    OpenGL C#绘图环境配置
  • 原文地址:https://www.cnblogs.com/songliquan/p/3639568.html
Copyright © 2020-2023  润新知