在登录事件中
c_characterManipulation MDI_characterManipulation = new c_characterManipulation();
//创建c_characterManipulation的类对象
if (txt_name.Text.Trim() == "")
{
MessageBox.Show("用户名不能为空,请重新输入!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else if (txt_pwd.Text == "")
{
MessageBox.Show("密码不能为空,请重新输入!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else if (txt_name.Text.Trim().Length < 3 && txt_name.Text.Trim().Length > 16)
//设定用户名的长度在4-16之间
{
MessageBox.Show("用户名长度应该在4-16个字符之间!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else if(txt_pwd.Text.Length<3 && txt_pwd.Text.Length>16)
//设定密码长度在4-16之间
{
MessageBox.Show("密码长度应该在6-16个字处之间!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else if (!MDI_characterManipulation.b_IfCharacter(txt_name.Text.Trim()))
//判断用户名输入是否合法
{
MessageBox.Show("用户名不合法,请求重新输入!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (MDI_characterManipulation.if_blank(txt_name.Text.Trim()))
{
MessageBox.Show("用户名中不能有空格,重新输入!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txt_name.Text = "";
txt_pwd.Text = "";
}
else if (!MDI_characterManipulation.b_IfCharacter(txt_pwd.Text))
//密码不能有单引号
{
MessageBox.Show("用户名、密码错误,请重新输入!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txt_name.Text = "";
txt_pwd.Text = "";
}
else
{
c_login MDI_Login = new c_login(txt_name.Text.Trim(), MDI_characterManipulation.str_Apostrophe(txt_pwd.Text));
//创建c_login的类对象
if (!MDI_Login.b_userVerification())
//判断用户信息表中有没有此用户
{
if (!MDI_Login.b_Students())
//判断学生信息表中有没有此学号
{
MessageBox.Show("用户名或密码错误,请重新输入!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
txt_name.Text = "";
txt_pwd.Text = "";
}
else if (!MDI_Login.b_Spwd())
//判断学生密码是不是正确
{
MessageBox.Show("用户名或密码错误,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txt_name.Text = "";
txt_pwd.Text = "";
}
else
{
Frm_MDI MDI = new Frm_MDI(txt_name.Text.Trim());
//创建“Frm_MDI”对象,同时将用户名输入框中的内容传到“Frm_MDI”页面
this.Hide();
//隐藏当前窗口
MDI.Show();
//显示“Frm_MDI”窗口
}
}
else if (!MDI_Login.b_pwdVerification())
{
MessageBox.Show("用户名或密码错误,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
Frm_MDI MDI = new Frm_MDI(txt_name.Text.Trim());
this.Hide();
MDI.Show();
}
}
c_login类
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
//所有的一般数据访问类
using System.Data.Sql;
//用于SQL Server数据访问的通用新接口和类
using System.Data.SqlClient;
//SQL Server提供程序的类
using System.Data.SqlTypes;
//SQL Server数据类型
using System.Data.Odbc;
using System.Configuration;
//在项目中要添加对System.Configuration的引用
namespace StudentManagementSystem
{
class c_login
{
SqlConnection conn = new SqlConnection("server=.;pwd=sun;user id=sa;database=SIM");
//数据库连接
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
string str_userName;
string str_pwd;
public c_login(string username,string pwd)
{
str_userName = username.Trim();
//接收用户框输入的用户名
str_pwd = pwd;
//接收用户框输入的密码
}
public bool b_Students()
//判断登录名是不是学号
{
bool b_TF;
try
{
conn.Open();
= "select t_number from tb_studentInfo where t_number='"+ str_userName +"'";
//按用户名查找学号
cmd.Connection = conn;
dr = cmd.ExecuteReader();
if (dr.Read())
{
b_TF = true;
}
else
{
b_TF = false;
}
conn.Close();
return b_TF;
}
finally
{
conn.Close();
}
}
public bool b_Spwd()
//按学好查找密码
{
bool b_TF;
try
{
conn.Open();
= "select t_pwd from tb_studentInfo where t_number='" + str_userName + "'";
//按学好查密码
cmd.Connection = conn;
dr = cmd.ExecuteReader();
if (dr.Read())
{
b_TF = true;
}
else
{
b_TF = false;
}
conn.Close();
return b_TF;
}
finally
{
conn.Close();
}
}
public bool b_userVerification()
//判断用户名正确性,查到后返回真
{
bool b_user;
try
{
conn.Open();
= "select t_UserName from tb_UserInfo where t_UserName='" + str_userName + "' collate Chinese_PRC_CS_AI_WS ";
//查找用户名时区分大小写、不区分重音、区分宽度
cmd.Connection = conn;
dr = cmd.ExecuteReader();
//执行SQL查询语句
if (dr.Read())
{
b_user = true;
}
else
{
b_user = false;
}
conn.Close();
}
finally
{
conn.Close();
}
return b_user;
}
public string
str_pwdVerification()
//返回数据库中的密码
{
string strPwd = "";
try
{
conn.Open();
= "select t_pwd from tb_UserInfo where t_UserName='" + str_userName + "' and t_pwd='" + str_pwd + "' collate Chinese_PRC_CS_AI_WS ";
//查找密码时区分大小写、不区分重音、区分宽度
cmd.Connection = conn;
dr = cmd.ExecuteReader();
if (dr.Read())
{
strPwd = dr["t_pwd"].ToString();
//返回密码字段
}
else
{
strPwd = "";
}
conn.Close();
}
finally
{
conn.Close();
}
return strPwd;
}
public bool b_pwdVerification()
//判断密码正确性,查到后返回真
{
bool b_pwd;
try
{
conn.Open();
= "select t_pwd from tb_UserInfo where t_UserName='" + str_userName + "' and t_pwd='" + str_pwd + "' collate Chinese_PRC_CS_AI_WS ";
//查找密码时区分大小写、不区分重音、区分宽度
cmd.Connection = conn;
dr = cmd.ExecuteReader();
//执行SQL查询语句
if (dr.Read())
{
b_pwd = true;
}
else
{
b_pwd = false;
}
conn.Close();
}
finally
{
conn.Close();
}
return b_pwd;
}
}
}
c_characterManipulation这个类就算了,你把关于这个类中的方法判断的全删掉就可以了,我在我的项目上考来的,高手给看看怎么改,效率会更高!