SQL Server 2019:
以下是我用到的数据库命名
| 数据库 | CRCS |
|---|---|
| 数据库表 | UserInfo |
数据库表设计如下
Visual Stuio 2019
同一框架为 .NET Framework 4.8
创建一个winform窗体 以及其他类库
| 输出类型 | 程序集名称 | 引用层次 |
|---|---|---|
| windows应用程序 | UI | Facade层、 Entity层 |
| 类库 | IDAL | Entity层 |
| 类库 | Factory | IDAL层 |
| 类库 | Facade | BLL层 、Entity层 |
| 类库 | Entity | |
| 类库 | DAL | IDAL层、Entity层 |
| 类库 | BLL | Entity层、Factory层、IDAL层 |
winform窗体如下
所需控件
TextBox
Botton
窗体以及控件命名
| 用户名文本框 | txtUserName |
|---|---|
| 密码文本框 | txtPassWord |
| 按键 | butOK |
using System;
using System.Windows.Forms;
namespace UI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
///
/// 按钮点击事件
///
///
///
private void butOK_Click(object sender, EventArgs e)
{
//判断文本框是否为空
if (txtUserName.Text.Trim() == "")
{
MessageBox.Show("请输入你的用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (txtPassWord.Text == "")
{
MessageBox.Show("请输入你的密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
//实例化对象 装饰层对象|实体层对象
Facade.LoginFacade zhuangshi = new Facade.LoginFacade();
Entity.UserInfo yonghu = new Entity.UserInfo();
//实体层对象接受文本框的值 yonghu 是Entity层包装起来的对象{userid,password,level,username}
yonghu.UserName = txtUserName.Text;
yonghu.PassWord = txtPassWord.Text;
//定义布尔值开始进行后续操作
Boolean flag = false;
Facade.LoginFacade denglu = new Facade.LoginFacade();//实例化一个登录对象
//跳到Facade层,把yonghu 这个对象当做值传入
flag = denglu.SelectUser(yonghu);
//用布尔值判断是否登录成功
bool Flase = false;
if (flag != Flase)
{
MessageBox.Show("登录成功");
this.Hide();
this.DialogResult = System.Windows.Forms.DialogResult.OK;
Form a = new Form();
a.Show();
}
else
{
MessageBox.Show("用户名或密码不正确");
}
}
}
}
}
UI层配置文件
IDAL层//这里用的是验证登录
using Entity;
using System.Data;
namespace IDAL
{
//接口层
public interface LoginIDAL
{
//由DAL层的LoginDal类实现接口
DataTable SelectUser(UserInfo yonghu);
}
}
Factory
using System.Reflection;
namespace Factory
{
//在BLL层中实例化
public class LoginFactory
{
//配置文件在UI层
string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
//在BLL层中调用生成一个DAL的LoginDal类
public IDAL.LoginIDAL CreateUser()
{
string ClassName = StrDB + "." + "LoginDal";
return (IDAL.LoginIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);
}
}
}
Facade 层
using System;
namespace Facade
{
///
/// 在UI层中生成的对象
///
///
///
public class LoginFacade
{
//从UI层过来,将Entity层中生成的对象 yonghu 值是 {userid,password,level,username} 传入
public Boolean SelectUser(Entity.UserInfo yonghu)
{
bool flag;
BLL.LoginBll userbll = new BLL.LoginBll();//实例化BLL对象
flag = userbll.UserBll(yonghu);//跳到BLL层,调用用BLL层的方法UserBLL,得到一个布尔值
return flag;//返回布尔值给UI层
}
}
}
Entity层
namespace Entity
{
public class UserInfo
{
//定义 用户ID
private int userid;
public int UserId
{
get { return userid; }
set { userid = value; }
}
//定义用户密码
private string password;
public string PassWord
{
get { return password; }
set { password = value; }
}
//定义用户类型字段
private string level;
public string Level
{
get { return level; }
set { level = value; }
}
//定义用户名字
private string username;
public string UserName
{
get { return username; }
set { username = value; }
}
}
}
BLL层
using System.Data;
namespace BLL
{
//在Facade中生成的实例
public class LoginBll
{
//由Facade层调用的方法, 值是 Entity层 生成的对象 yognhu 值是 {userid,password,level,username} 传入
public bool UserBll(Entity.UserInfo yonghu)
{
//跳到Factory层去实例化一个工厂
Factory.LoginFactory fact = new Factory.LoginFactory();
//调用工厂方法生成的DAL.LoginDal类实现接口 JieKou(接口)
IDAL.LoginIDAL JieKou = fact.CreateUser();
//跳到DAL层的LoginDal类中,返回一个表赋值给table
//为什么会跳到DAL层,因为DAL层的LoginDal类实现了接口
DataTable table = JieKou.SelectUser(yonghu);
bool flag;
if (table.Rows.Count == 0)//返回数据表类型,如果行数=0,说明没有符合该账号密码的用户
{
flag = false;
}
else
{
flag = true;
}
return flag;//返回布尔值给Facade层
}
}
}
DAL层
LoginDal类
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
//在Factory层中产生
public class LoginDal : IDAL.LoginIDAL
{
//在BLL层进行调用
public DataTable SelectUser(Entity.UserInfo yonghu)
{
SqlHelper sqlHelper = new SqlHelper();//实例化DAL层中的SqlHelper类
//映射,具体参考MSDN这个类的介绍
SqlParameter[] sqlparams =
{
new SqlParameter(@"UserName",yonghu.UserName),
new SqlParameter(@"PassWord",yonghu.PassWord)
};
//真正在数据库中进行操作的语句,匹配数据库表,参考sql server语句使用方法
string sql = @"SELECT * FROM UserInfo WHERe userid=@UserName AND password=@PassWord";
//跳到DAL层中的SqlHelper类的方法ExecuteQuery 含有3个参数
DataTable dt = sqlHelper.ExecuteQuery(sql, sqlparams, CommandType.Text);
return dt;//返回给BLL层数据表
}
}
}
DAL层
SqlHelper类
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
//数据访问层,数据操作类
public class SqlHelper
{
//定义数据库连接操作,指定在数据库上操作的类型
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataReader sdr = null;//流的方式读取数据库内容
///
/// 创建连接语句
///
public SqlHelper()
{
string connStr = ConfigurationManager.AppSettings["conStr"];
conn = new SqlConnection(connStr);
}
///
/// 判断数据库是否已经连接
///
///
private SqlConnection GetConn()
{
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
return conn;
}
///
///执行不带参数的数据库操作或者存储过程
/// 增删改查操作
/// 返回受影响的行数
///
public int ExecuteNonQuery(string cmdText, CommandType ct)
{
int res;
cmd = new SqlCommand(cmdText, GetConn());//命令语句
cmd.CommandType = ct;//?
res = cmd.ExecuteNonQuery();
if (conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
}
return res;
}
//执行带参数的数据库操作或者存储过程
public int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
int res;
using (cmd = new SqlCommand(cmdText, GetConn()))
{
cmd.CommandType = ct;
cmd.Parameters.AddRange(paras);
res = cmd.ExecuteNonQuery();
}
return res;
}
//执行不带参数的SqL查询语句或者存储过程
public DataTable ExecuteQuery(string cmdText, CommandType ct)
{
DataTable dt = new DataTable();
cmd = new SqlCommand(cmdText, GetConn());
cmd.CommandType = ct;
using (sdr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
//执行带参数的SQL查询语句或存储过程
//有DAL层的LoginDal类中进行调用
public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
DataTable dt = new DataTable();//创建一个新表
cmd = new SqlCommand(cmdText, GetConn());//数据库命令操作,实际上就是进行数据库语句
cmd.CommandType = ct;//commandType 代表要执行的类型 commandTyop.Text 是以SQl语句进行查询 在LoginDal类中传入的
//sql语句或存储过程的参数 各个参数到dataset的映射
//向 SqlParameterCollection 的末尾添加一个值数组。
cmd.Parameters.AddRange(paras);
//sdr = cmd.ExecuteReader()解释 生成一个只读的对象
//CommandBehavior.CloseConnection 作用:返回对象前,数据库不能被关闭
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);//填充表 //感觉好像sdr是一张表,然后贴到了dt表上
}
return dt;//返回给DAL层LoginDal类一个数据表
}
}
}
可能出现的问题 & 解决方案
一、部分类的使用要用到系统的命名空间
例如:
| System.Data; |
|---|
| System.Reflection; |
| using System.Data.SqlClient; |
| using System.Configuration; |
需要添加引用
二、建立层的时候一定要注意,除了UI层其他层都是创建的类库
三、数据库的连接和操作方法
资料来源https://blog.csdn.net/qq_30631063/article/details/91592006



