创建数据库
首先我们先根据图片要求创建一个数据库
--进入系统数据库master
use master
go
--判断数据Emp是否存在,如果存在,就删除
if exists(select * from sysdatabases where name = 'Emp')
drop database Emp
go
--创建数据库
create database Emp
go
--进入你创建的数据库
use Emp
go
--创建表
create table Emplorer
(
UserID int not null identity(1,1) primary key,
Account Varchar(200),
Password Varchar(50),
UserName Varchar(50),
Address Varchar(200),
Sex int not null
)
go
--添加数据
insert into Emplorer(Account,Password,UserName,Address,Sex)values('admin','123','张三','地球',1)
insert into Emplorer(Account,Password,UserName,Address,Sex)values('user','321','李四','火星',2)
insert into Emplorer(Account,Password,UserName,Address,Sex)values('aaa','123','王五','木星',1)
insert into Emplorer(Account,Password,UserName,Address,Sex)values('bbb','321','老六','金星',2)
打开编译器VS搭建三层框架
先创建新的ASP.NET Web应用程序(.NET Framework)项目取名Emplorer(名称自己选)
选择MVC进行创建
然后右键解决方案 =》添加 =》 新建项目 =》 类库
此处应该注意:
①一定是右键 解决方案 而不是右键项目名来进行添加
②不同版本的VS 他的类库种类不一样 此处我们选择的类库是 类库(.NET Fromwork)
用以上方法分别建立 DAL BLL Model三个类库 并将其中的所有生成的类给删除
然后开始添加引用
点击DAL =》右键引用 =》添加引用 =》点击项目 =》点击Model =》点击确定
点击BLL =》 右键引用 =》添加引用 =》 点击项目 =》点击DAL和Model =》点击确定
点击Emplorer(也叫UI层和MVC层)=》 右键引用 =》添加引用 =》 点击项目 =》点击BLL和Model =》点击确定
注意:因为Model是用来传递数据的 所以每一个层都要引用它
然后我们要创建一个类用来引用数据库
右键点击DAL =》 添加 =》类 =》名称叫 DBHelper
首先我们再class前添加public 然后添加以下代码
/// <summary>
/// 更新操作:增,删,改 共用
/// </summary>
/// <param name="sql"></param>
/// <returns>bool</returns>
public static bool UpdateOpera(string sql, params SqlParameter[] sps)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(sps);
return cmd.ExecuteNonQuery() > 0;
}
/// <summary>
/// 多行查询操作:返回SqlDataReader
/// </summary>
/// <param name="sql">查询SQL语句</param>
/// <returns>SqlDataReader</returns>
public static SqlDataReader GetReader(string sql)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
/// <summary>
/// 多行查询操作:返回DataTable
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataTable GetDataTable(string sql)
{
DataTable dt = new DataTable();
SqlDataAdapter dad = new SqlDataAdapter(sql, Connection);
dad.Fill(dt);
return dt;
}
/// <summary>
/// 查询操作:返回首行首列数据
/// </summary>
/// <param name="sql">查询SQL语句</param>
/// <returns>object</returns>
public static object GetScalar(string sql)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
return cmd.ExecuteScalar();
}
private static SqlConnection _connection;
/// <summary>
/// Connection对象
/// </summary>
public static SqlConnection Connection
{
get
{
//Data Source=.;Initial Catalog=StuDB;Persist Security Info=True;Integrated Security=True
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnStr"].ConnectionString;
if (_connection == null)
{
_connection = new SqlConnection(connectionString);
_connection.Open();
}
else if (_connection.State == ConnectionState.Closed)
{
_connection.Open();
}
else if (_connection.State == ConnectionState.Broken || _connection.State == ConnectionState.Open)
{
_connection.Close();
_connection.Open();
}
return _connection;
}
}
当你添加完后会发现有报错的现象,这时候我们应该点击bug处点灯泡添加引用
添加完两个using后 发现还剩下最后一处bug了
此处bug是一个在config里面配置的一个connection
接下来我们就点击项目名Emplorer =》找到Web.config 并点击它
然后我们在进行添加连接字符串connectionStrings
其中在
connectionString是 点击工具 =》连接到数据库
最后在进行添加Configuration引用
因为我们是在DAL当中,所以是在DAL里面进行添加
点击DAL =》 右键引用 =》添加引用 =》点击程序集 =》找到并点击system.Configuration=》点击确定
右键解决方案 =》重新生成解决方案
如果此时没有红线或者红点表示bug暂时没有
接下来我们就在Model层里面 有几张表就封装几个类
因为我们现在的数据库里面只有一个表Emplorer所以现在只需要封装一个类名字叫EmplorerModel
然后在class前添加public 最后就是来封装了
namespace MODEL
{
public class EmplorerModel
{
//这是把数据库里面的字段拉过来方便我们去封装
//UserID int not null identity(1,1) primary key,
//Account Varchar(200),
//Password Varchar(50),
//UserName Varchar(50),
//Address Varchar(200),
//Sex int not null
//这里有一个快捷键prop+tab键自动生成然后进行修改
public int UserID { get; set; }
public string Account { get; set; }
public string Address { get; set; }
public string Password { get; set; }
public string UserName { get; set; }
public int Sex { get; set; }
}
}
到了这里,我们的准备工作已经完成了。接下来就是要开始做登录了
登录
登录页面的制作
首先我们在项目的Controllers里添加一个控制器
选择空的控制器进行添加
注意:添加的时候名字的创建 只能修改 Controller 前面的字段 不要进行不必要的修改
然后我们将名字改成LoginController 点击添加
然后我们来添加页面
点击Index =》选中后右击 =》点添加视图
=》选择视图添加
=》选项都不进行勾选 =》点击添加
然后我们就可以在Views文件夹中的Login文件夹里找到我们刚刚建好的Index视图
然后开始建造我们的登录页面
<div>
<h1>
登陆页面
</h1>
<form action="/" method="post">
<table>
<tr>
<td>用户名:</td>
<td>
<input type="text" name="account" value="" />
</td>
</tr>
<tr>
<td>密 码:</td>
<td>
<input type="text" name="pwd" value="" />
</td>
</tr>
<tr>
<td colspan="2">
<button>登录</button>
</td>
</tr>
</table>
</form>
</div>
当我们的页面搭建完后,这时我们就可以开始走三层了
登录的DAL语句
先走DAL调用数据库 那么DAL调用数据库 你要控制哪一张表 那么你就建一张这个表的类
因为我们现在只有Employrer一张表所以我们只需要建一个叫EmplorerDAL的类来控制它
右击DAL =》添加 =》新建项 =》选择类 =》名字改成EmplorerDAL =》点击添加
首先第一步就是在class前添加public
然后进行代码的敲写
//因为我们封装了类所以我们用实体类传输数据的方式
//所以括号里的是MODEL实体类里的EmplorerModel 然后我们给他一个名字叫做emp
//这样我们就可以拿到emp里的所有字段
public static DataTable Login(MODEL.EmplorerModel emp)
{
//string sql = @""这里是数据库里的查询语句"
string sql = @"select UserID,Account,Password,UserName,Address,Sex from Emplorer
where Account = '" + emp.Account + "' and Password = '" + emp.Password + "'";
DataTable dt = DBHelper.GetDataTable(sql);
return dt;
}
但是因为我们最终想要得到的数据是list而不是DataTable
DataTable非常不灵活,所以我们就要利用一个方法把dt转成我们需要的List
public static List<MODEL.EmplorerModel> GetList(DataTable dt)
{
//序列化成json字符串,需要引用newtonsoft
string JsonStr = JsonConvert.SerializeObject(dt);
//反序列化成list
List<MODEL.EmplorerModel> list = JsonConvert.DeserializeObject<List<MODEL.EmplorerModel>>(JsonStr);
return list;
}
其中引用newtonsoft需要自己找 此处可以参考网上的教程引用newtonsoft
然后我们就可以对Login进行修改
public static List<MODEL.EmplorerModel> Login(MODEL.EmplorerModel emp)
{
string sql = @"select UserID,Account,Password,UserName,Address,Sex from Emplorer
where Account = '" + emp.Account + "' and Password = '" + emp.Password + "'";
DataTable dt = DBHelper.GetDataTable(sql);
return GetList(dt);
}
登录的BLL语句
我们先在BLL里面添加新的类名字就叫EmplorerBLL(创建方式参考上文DAL的创建)
然后还是一样的现在class前添加public
public static List<MODEL.EmplorerModel> Login(MODEL.EmplorerModel emp)
{
return DAL.EmplorerDAL.Login(emp);
}
控制器的登录语句
这是我们就要对controllers里面的Logincontroller进行添加
首先我们得有一个Index页面处理的方法
public ActionResult IndexExe()
{
return View();//先不让它报错
}
然后我们再回到页面上去
因为我们在页面上输入了账号密码,点击了button后我们是要将数据传到我们的处理方法上
所以我们再
public ActionResult IndexExe()
{
//新建两个对象来获取页面上的账号密码
string account = Request.Form["account"];
string pwd = Request.Form["pwd"];
//实体类传递数据 , 一定有一个实体类
MODEL.EmplorerModel emp = new MODEL.EmplorerModel()
{
Account = account,
Password = pwd
};
//调用BLL
List<MODEL.EmplorerModel> list = BLL.EmplorerBLL.Login(emp);
//判断
if (list.Count > 0 ) {
//跳转到显示页面
return Redirect("/Emp/Index");
}
else {
//用户名或密码错误
return Content("用户名或密码错误");
}
}
因为我们还没有页面所以我们再敲完后再到Controllers里新建一个EmpController
并添加视图 使我们登录完可以经行页面跳转
Q.E.D.