登录运用的是数据库中查询操作,在下面SQLHelper中会介绍查询与其他不同的地方
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entity
{
public class UserInfo
{
//定义用户ID字段
public int UserID { set; get; }
//定义用户名字段
public string UserName { set; get; }
//定义密码字段
public string PassWord { set; get; }
//定义等级字段
public string Level { set; get; }
//定义状态字段
public string state { set; get; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace IDAL
{
public interface LoginIDAL
{
DataTable selectUser(Entity.UserInfo UserInfo);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;//操作Sql Server数据库,需要引用
using System.Configuration;
namespace DAL
{
public class LoginDAL:IDAL.LoginIDAL
{
public DataTable selectUser(Entity.UserInfo UserInfo)
{
//实例化一个SQLHelper
SQLHelper sqlHelper = new SQLHelper();
//定义需要传递的参数
SqlParameter[] sqlParams = { new SqlParameter("@UserID", UserInfo.UserID), new SqlParameter("@PassWord", UserInfo.PassWord)};
string sql = @"SELECT * FROM [User_Info] WHERE UserID=@UserID and PWD =@PassWord ";
DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
return table;
}
}
}
代码请参考【C#】机房重构——七层登录
SQLHelper中为什么把查询和增删改的SQL语句分开,请看下图
写在UI层中的App.config中,为了便于日后的程序维护
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
startup>
<appSettings>
<add key="ConnStr" value="Server=GHZ\SQLEXPRESS; Database=JiFang; User ID=sa;Password=123456"/>
<add key="DB" value="DAL"/>
appSettings>
configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Reflection;
namespace Factory
{
public class LoginFactory
{
//接受来自配置文件的数据
string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
public IDAL.LoginIDAL CreateUser()
{
//DAL层的类名
string ClassName = StrDB + "." + "LoginDAL";
//反射加工厂的应用
return (IDAL.LoginIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
public class LoginBLL
{
public Boolean UserBLL(Entity.UserInfo UserInfo)
{
//实例化工厂
Factory.LoginFactory fact = new Factory.LoginFactory();
//调用工厂方法创建接口
IDAL.LoginIDAL idal = fact.CreateUser();
//接收D层的返回值
DataTable table = idal.selectUser(UserInfo);
bool flag;
//返回的DataTable类型,如果他的行数等于0,说明没有符合该账号密码的账户
if (table.Rows.Count == 0)
{ flag = false; }
else
{ flag = true;
}
return flag;
}
public DataTable LevelBLL(Entity.UserInfo UserInfo)
{
//实例化工厂
Factory.LoginFactory fact = new Factory.LoginFactory();
//调用工厂方法创建接口
IDAL.LoginIDAL idal = fact.CreateUser();
//接收D层的返回值
DataTable level = idal.selectUser(UserInfo);
return level;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BLL;
using System.Data;
namespace Facade
{
public class LoginFacade
{
public Boolean SelectUser(Entity.UserInfo user)
{
bool flag;
//实例化B层
LoginBLL userBLL = new LoginBLL();
flag = userBLL.UserBLL(user);
//返回布尔flag
return flag;
}
public DataTable SelectLevel(Entity.UserInfo level1)
{
LoginBLL levelBLL = new LoginBLL();
DataTable level = levelBLL.LevelBLL(level1);
//返回DataTable level
return level;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UI
{
public partial class UILogin : Form
{
public UILogin()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
if (txtUserID.Text.Trim() == "")
{
MessageBox.Show("请输入账号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (txtPWD.Text == "")
{
MessageBox.Show("请输入密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
try
{
//实例化登录外观将参数传递给外观层
Facade.LoginFacade Facade = new Facade.LoginFacade();
Entity.UserInfo user = new Entity.UserInfo();
user.UserID = Convert.ToInt32(txtUserID.Text.Trim());
user.PassWord = txtPWD.Text;
Boolean flag = false;
//实例化外观
Facade.LoginFacade FLogin = new Facade.LoginFacade();
//调用外观的方法,返回给user
flag = FLogin.SelectUser(user);
if (flag != false)
{
//隐藏当前窗体
this.Hide();
this.DialogResult = System.Windows.Forms.DialogResult.OK;
DataTable level = FLogin.SelectLevel(user);
if (level.Rows[0][2].ToString() == "学生" )
{
//实例化窗体
frmStudent frmstu = new frmStudent();
//显示实例化的学生界面
frmstu.Show();
}
else if (level.Rows[0][2].ToString() == "操作员")
{
frmOpert frmopert = new frmOpert();
frmopert.Show();
}
else
{
frmAdmin frmadmin = new frmAdmin();
frmadmin.Show();
}
}
else
{
MessageBox.Show("密码或者用户名错误");
}
}
//如果try中代码出现异常,进入catch中
catch (Exception)
{
throw;
}
}