在学习三层的的这几天来在网上找的资料大多都是简单的例子,代码虽然很简单但是在实现的时候还是遇到了很多的问题。分析了一下原因我觉得是因为刚开始接触三层所以很多的基础知识都不知道。前一段时间结束了三层的一个登录小例子在这里进行一个简单的总结。麻雀虽小五脏俱全,旨在将一些基础的三层知识介绍给刚刚入门的同学们,高手请绕过!
登录例子中是一个Window窗体应用程序,所以需要建立的是:UI BLL DAL Entity
1. 搭建三层架构
我用的是Visual Studio2010,其他版本的步骤是一样的。
建立空白的解决方案
添加第一个项目
在解决方案资源管理器中右击“解决方案’登录’”;
创建窗体用户界面应用程序,选择是使用的语言C#,建立UI层
添加第二个项目
和第一个添加不一样,需要在文件-新建项目 解决方案中选择 添加到解决方案
解决方案的两个选项
1.创建新的解决方案
2.添加到解决方案
如果选择新建一个新的解决方案那么就会另外建立一个新的;如果是添加到另外一个那么就会将新建立的项目添加到已经建立的解决方案中
出现下面的窗体的时候选择 “类库”,建立DAL层
添加其他的项目
和添加第二个项目一样的步骤
建立完成之后的结果:
添加引用
Entity是需要添加引用(被引用)
DAL添加Entity
BLL添加DAL Entity
UI添加 Entity BLL
2. 各个层的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; //创建表类型的变量需要引用的空间
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity_LoginModel; //使用Entity_LoginModel的引用
using BLL_Login; //使用BLL_Login的引用
namespace UI_Login
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//实例化登录对象m,将文本框中输入的内容传给m
UserLogin m = new UserLogin();
m.AdminUser = this.textBox1.Text.ToString();
m.AdminPwd = this.textBox2.Text.ToString();
if (B_UserLogin.SysLogin(m) > 0)
{
this.label1.Text = "登陆成功,马上进入管理平台";
}
else
{
this.label1.Text = "用户名称或密码错误,请重新输入!";
}
}
}
}
Entity
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Entity_LoginModel
{
public class UserLogin
{
//两个属性adminUser adminPwd
private string adminUser;
private string adminPwd;
public string AdminUser
{
get { return adminUser ;}
set { adminUser = value; }
}
public string AdminPwd
{
get { return adminPwd; }
set { adminPwd = value; }
}
}
}
DAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient; //创建数据库连接需要引用的空间
using System.Data; //创建表类型的变量需要引用的空间
namespace DAL_Login
{
public class D_UserLogin
{
//数据库的连接 对数据的操作:增删 修改 更新 查找等操作
//数据库连接的函数
public static SqlConnection con()
{
return new SqlConnection("Data Source=PF-PC;Initial Catalog=userlogin ;User ID=sa;PassWord=123456;");
}
//查询方法 返回受影响的行数
//参数:存储过程名 存储过程参数
public string ExecuteScalar(string str, SqlParameter[] sql)
{
SqlConnection con = D_UserLogin.con();
try
{
con.Open();
//定义执行一个存储过程
SqlCommand com = new SqlCommand(str, con);
com.CommandType = CommandType.StoredProcedure;
//定义存储过程参数
com.Parameters.AddRange(sql);
//返回受影响的行数(例如影响了行数为1,那么返回数值1到BLL层,然后BLL层将数值1返回到UI层)
return Convert.ToString(com.ExecuteScalar());
}
catch (Exception Error)
{
throw Error;
}
finally
{
con.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity_LoginModel; //使用引用的Entity_LoginModel
using System.Data.SqlClient;//数据库连接需要引用的类库
using DAL_Login; //使用引用的DAL_Login
namespace BLL_Login
{
public class B_UserLogin
{
//实现具体的功能
//功能:用户登录
public static int SysLogin(UserLogin m)
{
string str="adminValid";//存储过程名称
SqlParameter[] sqlParameter=
{
//将UI层传递过来的用户名称和密码赋值给存储过程中的变量
//分别是adminUser和adminPwd
new SqlParameter ("adminUser",m.AdminUser),
new SqlParameter ("adminPwd",m.AdminPwd)
};
D_UserLogin d=new D_UserLogin();
return Int32.Parse(d.ExecuteScalar(str,sqlParameter));
}
}
}
3. 数据库的代码
用到的是SQL Server2008,在这个里面需要添加一个存储过程
存储过程的建立过程(省略的建立数据库的过程)
选中LoginUser数据库,点击新建查询;然后输入下面的代码
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
alter proc adminValid
@adminUser varchar(50),@adminPwd varchar(50)
as
select count(*) from userlogin where adminUser = @adminUser AND adminPwd = @adminPwd
4. 运行结果