基于C#.NET的WinForm项目,我们经常使用基于三层架构,来构建项目框架,这里简单的梳理一下三层架构的相关知识
我们通常所说的三层框架指的是DAL、BIL和UIL三层,分别是数据层、业务逻辑层和界面层,以及与之搭配的实体类和通用类库,下面分别概述
如图,我们定义了一个数据表,表结构如下
表结构
namespace mtWeightModel
{
[Serializable] //表示可以序列化
public class User
{
public int Id { get; set; }
public String username { get; set; }
public String password { get; set; }
public int status { get; set; }
}
}
namespace mtWeightDAL
{
///
/// 用户访问数据类
///
public class UserService
{
///
/// 根据账号和密码比对用户信息
///
/// 包含用户名和密码的用户对象
/// 返回用户对象信息(若无用户信息则对象为null)
public User UserLogin(User objUser) {
String sql = "SELECT Id,username,password,status FROM Users where username=@username and password=@password";
SqlParameter[] param = new SqlParameter[] {
new SqlParameter("@username",objUser.username),
new SqlParameter("@password", objUser.password)
};
SqlDataReader objReader = SqlHelper.getReader(sql, param);
if (objReader.Read())
{
objUser.Id = Convert.ToInt32(objReader["Id"]);
objUser.status = Convert.ToInt32(objReader["status"]);
}
else {
objUser = null;
}
objReader.Close();
return objUser;
}
}
}
namespace mtWeightBLL
{
///
/// 用户业务逻辑类
///
public class UserManager
{
//创建数据访问对象
private UserService objUserService = new UserService();
public User UserLogin(User objUser) {
return objUserService.UserLogin(objUser);
}
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
//数据验证
if (this.txtUsername.Text.Trim().Length == 0) {
MessageBox.Show("请输入用户名", "登录提示");
this.txtUsername.Focus();
}
if (this.txtPassword.Text.Trim().Length == 0)
{
MessageBox.Show("请输入密码", "登录提示");
this.txtPassword.Focus();
}
// 封装对象
User objUser = new User {
username = this.txtUsername.Text.Trim(),
password = this.txtPassword.Text.Trim()
};
try
{
objUser = objUserManager.UserLogin(objUser);
if (objUser != null)
{
if (objUser.status == 1)
{
Program.objCurrentUser = objUser;
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
MessageBox.Show("用户被禁用,请联系管理员", "登录提示");
}
}
else {
MessageBox.Show("用户名或密码错误!", "登录提示");
}
}
catch (Exception ex)
{
MessageBox.Show("登录异常:"+ex.Message,"登录提示");
}
}
引用关系图
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DbUtil
{
public class SqlHelper
{
private static String ConnString = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
#region 格式化SQL语句
///
/// 增删改非查询类方法
///
/// SQL语句
///
public static int UPDATE(string sql) {
SqlConnection conn = new SqlConnection(ConnString);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally {
conn.Close();
}
}
///
/// 返回单条结果查询类方法
///
/// SQL语句
///
public static object getSingleResult(string sql)
{
SqlConnection conn = new SqlConnection(ConnString);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
return cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Close();
}
}
///
/// 多条结果查询类方法
///
/// SQL语句
///
public static SqlDataReader getReader(string sql)
{
SqlConnection conn = new SqlConnection(ConnString);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
conn.Close();
throw new Exception(ex.Message);
}
}
///
/// 返回DataSet数据集方法
///
/// SQL语句
/// 结果集DataSet
public static DataSet getDataSet(string sql)
{
SqlConnection conn = new SqlConnection(ConnString);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
conn.Open();
adapter.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally {
conn.Close();
}
}
#endregion
#region 带参数SQL语句
///
/// 增删改非查询类方法
///
/// SQL语句
/// SQl参数
///
public static int UPDATE(string sql,SqlParameter[] param)
{
SqlConnection conn = new SqlConnection(ConnString);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
cmd.Parameters.AddRange(param);
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Close();
}
}
///
/// 返回单条结果查询类方法
///
/// SQL语句
/// SQL参数
///
public static object getSingleResult(string sql, SqlParameter[] param)
{
SqlConnection conn = new SqlConnection(ConnString);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
cmd.Parameters.AddRange(param);
return cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Close();
}
}
///
/// 多条结果查询类方法
///
/// SQL语句
/// SQL参数
///
public static SqlDataReader getReader(string sql,SqlParameter[] param)
{
SqlConnection conn = new SqlConnection(ConnString);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
cmd.Parameters.AddRange(param);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
conn.Close();
throw new Exception(ex.Message);
}
}
#endregion
}
}
引用完成后: