VWG的登录

数据库是MySQL,上面是表,表名为:user

在此数据表上有个存储过程:存储过程check0

BEGIN
 #Routine body goes here...
SELECT @id := a.id,@password := a.password,@loginname := a.username
from user a
where a.username=loginname_p;
IF FOUND_ROWS() = 0 THEN
 SET re_p = 0;
ELSEIF @password <> password_p THEN
 set re_p = 1;
ELSE
 set re_p = 2;
END IF;
END

以下是简单的登录代码,其中判断了用户名和密码

#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Data.SqlClient;
using System.Configuration;
using MySql.Data.MySqlClient;

#endregion

namespace myfirstVWG
{
    public partial class logon : Form
    {
        public logon()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 清空
        /// </summary>
        private void btnclear_Click(object sender, EventArgs e)
        {
            tb1.Text="";
            tb2.Text="";
            errorProvider1.SetError(tb1,string.Empty);
            errorProvider1.SetError(tb2, string.Empty);
            //tb1.Update();
            //tb2.Update();

        }

        /// <summary>
        /// 登录
        /// </summary>
        private void btnlogin_Click(object sender, EventArgs e)
        {
            //判断用户名是否为空
            string sloginname = tb1.Text.Trim();
            tb1.Text = sloginname;
            sloginname = tb1.Text.ToLower();
            //tb1.Update();
            if (sloginname == string.Empty)
            {
                errorProvider1.SetError(tb1, "用户名不能为空");
                MessageBox.Show("用户名不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                errorProvider1.SetError(tb1, string.Empty);
            }
            //判断密码是否为空
            string spass = tb2.Text.Trim();
            tb2.Text = spass;
            //tb2.Update();
            if (spass == string.Empty)
            {
                errorProvider1.SetError(tb2, "密码不能为空!");
                MessageBox.Show("密码不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                errorProvider1.SetError(tb2, string.Empty);
            }

            //与数据库中的用户信息作比较
            int n = check(sloginname, spass);

            if (n == 0)
            {
                errorProvider1.SetError(tb1, "账户不存在!");
                MessageBox.Show("账户不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                errorProvider1.SetError(tb1, string.Empty);
                if (n == 1)
                {
                    errorProvider1.SetError(tb2, "密码不正确!");
                    MessageBox.Show("密码不正确!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else  //n==2,loginname and password is ok!                          
                {
                    errorProvider1.SetError(tb2, string.Empty);
                    //转到登陆的窗口
                    Context.Transfer(new hello());

                }
                //Context.Transfer(new hello());
            }
        }
       

        /// <summary>
        /// n==0 account is not exist
        /// n==1 password is not right
        /// n==2 account and password ok
        /// </summary>
         private int check(string sloginname, string spass)
        {
            //连接字符串
            string consting=ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
            int n = 0;

            using (MySqlConnection conn = new MySqlConnection(consting))
            {
                MySqlCommand cmd = new MySqlCommand("check0",conn); //这里的check0是存储过程名
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("loginname_p",MySqlDbType.VarChar,tb1.Text.Length);
                cmd.Parameters["loginname_p"].Value = sloginname;
                cmd.Parameters.Add("password_p",MySqlDbType.VarChar,tb2.Text.Length);
                cmd.Parameters["password_p"].Value = spass;
                cmd.Parameters.Add("re_p",MySqlDbType.Int32);
                cmd.Parameters["re_p"].Direction = ParameterDirection.Output;

                conn.Open();
                cmd.ExecuteNonQuery();
                n = Convert.ToInt32(cmd.Parameters["re_p"].Value);
                conn.Close();               
            }
            return n;
        }
       
    }
}

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值