数据库是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;
}
}
}