zoukankan      html  css  js  c++  java
  • 存储过程实现登录(.net)

    工作中,可能有时为了安全等的考虑,需要更多 的运用存储过程。有的公司甚至在登录一栏也会提出这样的要求,那么怎么用存储过程实现登录呢。好处就不用言名了,一个速度,一个就是安全系统更高。

    下面贴上:1.存储过程登录代码

    --实现登陆的存储过程

    if exists(select * from sys.objects where name='usp_Login')  

    drop proc usp_Login

    go

    create proc usp_Login  

    @name varchar(10),

     @pwd varchar(10),  

    @isLogin int output  

    --1 登陆成功 2用户名错误 3密码错误 4密码错误超过3次

    as  declare @times int  --错误次数

     --根据用户名是否存在  

    if exists(select * from [user] where uUserName=@name)   

    begin   

     select @times = uTimes from [user] where uUserName=@name   

     if(@times = 3)     --密码错误3次    

     set @isLogin=4    

    else   

      begin    

      if exists(select * from [user] where uUserName=@name and uPwd=@pwd)     

      begin      

      --用户名密码正确 登陆成功       

     set @isLogin=1       

     update [user] set uTimes=0 where uUserName=@name      

     end      

    else       

    begin     

       --密码错误       

     set @isLogin=3       

     update [user] set uTimes=uTimes + 1 where uUserName=@name      

     end     

    end  

     end  

    else   

    --用户名不存在   

    set @isLogin= 2

    declare @login int

    --调用登陆存储过程

    exec usp_Login 'admin','123',@login output

    print @login select * from [user]

    2,客户端代码如下。调用存储过程

    ///登录事件
            private void btnLogin_Click(object sender, EventArgs e)
            {
                int n = Login1(txtName.Text, txtPwd.Text);
                //--1 登陆成功 2用户名错误 3密码错误 4密码错误超过3次
                if (n == 1)
                {
                    MessageBox.Show("登陆成功");
                }
                else if(n == 2)
                {
                    MessageBox.Show("用户名错误");
                }
                else if (n == 3)
                {
                    MessageBox.Show("密码错误");
                }
                else if (n == 4)
                {
                    MessageBox.Show("密码错误超过");
                }
                else
                {
                    MessageBox.Show("未知错误");
                }
            }

    ///3.存储过程的调用

        private  int Login1(string name, string pwd) {    

             string connStr = @"Data Source = .\sqlexpress;Initial Catalog = MySchool;uid=sa;pwd=sa";   

              using (SqlConnection conn = new SqlConnection(connStr))  {

                    using (SqlCommand cmd = new SqlCommand()) {       

                  cmd.Connection = conn;                    

        cmd.CommandText = "usp_Login";  

                 cmd.CommandType = CommandType.StoredProcedure;                    

        cmd.Parameters.AddWithValue("@name", name);                    

        cmd.Parameters.AddWithValue("@pwd", pwd);

                        //输出参数                    

        SqlParameter sp = cmd.Parameters.Add("@isLogin", SqlDbType.Int);                    

        sp.Direction = ParameterDirection.Output;                    

           conn.Open();                   

            cmd.ExecuteNonQuery();   

                        //获取输出参数的值                   

            int result = Convert.ToInt32(sp.Value);

                        return result;               

              }         

          }        

    }

  • 相关阅读:
    vue ui 命令使用
    vue环境搭建
    简版的电商项目学习——第四步:从数据库获取数据,页面跳转以及跳转页面成功与否的提示信息设置
    简版的电商项目学习——第三步:数据库创建,注册功能实现以及用户密码加密
    简版的电商项目学习——第二步:页面布局,以及路由设置
    简版的电商项目学习——第一步:express后台搭建以及基本模块、插件配置
    sass 控制指令
    继承,混合器,占位符的用法 和 sass的数据类型
    ruby 安装 和 sass入门
    大数乘法,分治O(n^1.59)
  • 原文地址:https://www.cnblogs.com/renshen555/p/3508112.html
Copyright © 2011-2022 走看看