三层架构和MVC开发:
1.先对大致框架做一个简单的规划
2.在对具体类下新建具体的子类
3.分别编写具体代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using Models;
using System.Data;
namespace BLL
{
public class StudentManger
{
private StudentService dal = new StudentService();
public List<Student> GetStudentList()
{
return dal.GetStudentList().Count > 0 ? dal.GetStudentList() : null;
}
public bool AddStudent(Student stu)
{
return dal.AddStudent(stu)>0;
}
public bool UpdateStudent(Student stu)
{
return dal.UpdateStudent(stu)>0;
}
public bool DeleteStudent(int id)
{
return dal.DeleteStudent(id)>0;
}
public Student SelectStudent(int id)
{
return dal.SelectStudent(id);
}
}
}
sqlhelper类的编写(有的名字不同,但作用一样)
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
internal static class SqlHelper
{
//1.连接字符串
private static readonly string constr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
//2.执行增删改的
public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteNonQuery();
}
}
}
//3.执行返回单个值的
public static object ExecuteScalar(string sql, params SqlParameter[] pms)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteScalar();
}
}
}
//4.执行返回SqlDataReader
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)
{
SqlConnection con = new SqlConnection(constr);
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
try
{
con.Open();
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
catch (Exception)
{
con.Close();
con.Dispose();
throw;
}
}
}
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
{
DataTable dt = new DataTable();
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, constr))
{
if (pms != null)
{
adapter.SelectCommand.Parameters.AddRange(pms);
}
adapter.Fill(dt);
}
return dt;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
public class StudentService
{
public Student SelectStudent(int id)
{
string sql = "select * from student where id=@id";
Student stu = null;
using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter("@id", id)))
{
//封装数据
if (reader.Read())
{
stu = new Student();
stu.id = reader.GetInt32(0);
stu.stuid = reader.GetInt32(1);
stu.name = reader.GetString(2);
stu.age = reader.GetInt32(3);
stu.sex = reader.GetString(4);
stu.phone= reader.GetString(5);
stu.Class = reader.GetString(6);
stu.grad = reader.GetString(7);
}
}
return stu;
}
public int DeleteStudent(int id)
{
string sql = "delete from student where id=@id";
int count = SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@id", id));
return count;
}
public List<Student> GetStudentList()
{
string sql = "select * from student";
List<Student> studentList = new List<Student>();
using (SqlDataReader reader = SqlHelper.ExecuteReader(sql))
{
if (reader.HasRows)
{
while (reader.Read())
{
Student stu = new Student();
stu.id = reader.GetInt32(0);
stu.stuid = reader.GetInt32(1);
stu.name = reader.GetString(2);
stu.age = reader.GetInt32(3);
stu.sex = reader.GetString(4);
stu.phone = reader.GetString(5);
stu.Class = reader.GetString(6);
stu.grad = reader.GetString(7);
studentList.Add(stu);
}
}
}
//返回
return studentList;
}
public int UpdateStudent(Student stu)
{
string sql = "update student set stuid=@stuid,name=@name,age=@age,sex=@sex,phone=@phone,Class=@Class,grad=@grad where id=@id ";
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@id",stu.id),
new SqlParameter("@stuid",stu.stuid),
new SqlParameter("@name",stu.name),
new SqlParameter("@age",stu.age),
new SqlParameter("@sex",stu.sex),
new SqlParameter("@phone",stu.phone),
new SqlParameter("@Class",stu.Class),
new SqlParameter("@grad",stu.grad),
};
int count = SqlHelper.ExecuteNonQuery(sql, param);
return count;
}
public int AddStudent(Student stu)
{
string sql = "insert into student values(@stuid,@name,@age,@sex,@phone,@Class,@grad)";
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@stuid",stu.stuid),
new SqlParameter("@name",stu.name),
new SqlParameter("@age",stu.age),
new SqlParameter("@sex",stu.sex),
new SqlParameter("@phone",stu.phone),
new SqlParameter("@Class",stu.Class),
new SqlParameter("@grad",stu.grad),
};
int count = SqlHelper.ExecuteNonQuery(sql, param);
return count;
}
}
}
Models的编写,这是根据数据库中所建表所编写。这个id列为自增列
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Models
{
public class Student
{
public int id { get; set; }
public int stuid { get; set; }
public string name{ get; set; }
public int age { get; set; }
public string sex { get; set; }
public string phone { get; set; }
public string Class{ get; set; }
public string grad { get; set; }
}
}
在controllers下添加控制器,自己起名字,控制器设为空。自己编写内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Models;
using BLL;
namespace StudentMVC.Controllers
{
public class StudentMangerController : Controller
{
private StudentManger db = new StudentManger();
public ActionResult Index()
{
return View(db.GetStudentList());
}
public ActionResult Details(int id)
{
Student student = db.SelectStudent(id);
if(student==null)
{
return HttpNotFound();
}
return View(student);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Student student)
{
try
{
db.AddStudent(student);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Edit(int id)
{
Student student = db.SelectStudent(id);
if(student==null)
{
return HttpNotFound();
}
return View(student);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Student student)
{
if (db.UpdateStudent(student))
return RedirectToAction("Index");
else
return View(student);
}
public ActionResult Delete(int id)
{
Student student = db.SelectStudent(id);
if(student==null)
{
return HttpNotFound();
}
return View(student);
}
[HttpPost,ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Student student = db.SelectStudent(id);
db.DeleteStudent(id);
return RedirectToAction("Index");
}
}
}
在控制器下添加控制器后,会自动生成视图,自行添加视图(增,删,改,查).
@model Models.Student
@{
ViewBag.Title = "Create";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.stuid, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.stuid, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.stuid, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.age, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.age, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.age, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.sex, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.sex, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.sex, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.phone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Class, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Class, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Class, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.grad, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.grad, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.grad, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="创建" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
删
@model Models.Student
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Student</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.stuid)
</dt>
<dd>
@Html.DisplayFor(model => model.stuid)
</dd>
<dt>
@Html.DisplayNameFor(model => model.name)
</dt>
<dd>
@Html.DisplayFor(model => model.name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.age)
</dt>
<dd>
@Html.DisplayFor(model => model.age)
</dd>
<dt>
@Html.DisplayNameFor(model => model.sex)
</dt>
<dd>
@Html.DisplayFor(model => model.sex)
</dd>
<dt>
@Html.DisplayNameFor(model => model.phone)
</dt>
<dd>
@Html.DisplayFor(model => model.phone)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Class)
</dt>
<dd>
@Html.DisplayFor(model => model.Class)
</dd>
<dt>
@Html.DisplayNameFor(model => model.grad)
</dt>
<dd>
@Html.DisplayNameFor(model => model.grad)
</dd>
</dl>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index")
</div>
}
</div>
查
@model Models.Student
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Student</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.stuid)
</dt>
<dd>
@Html.DisplayFor(model => model.stuid)
</dd>
<dt>
@Html.DisplayNameFor(model => model.name)
</dt>
<dd>
@Html.DisplayFor(model => model.name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.age)
</dt>
<dd>
@Html.DisplayFor(model => model.age)
</dd>
<dt>
@Html.DisplayNameFor(model => model.sex)
</dt>
<dd>
@Html.DisplayFor(model => model.sex)
</dd>
<dt>
@Html.DisplayNameFor(model => model.phone)
</dt>
<dd>
@Html.DisplayFor(model => model.phone)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Class)
</dt>
<dd>
@Html.DisplayFor(model => model.Class)
</dd>
<dt>
@Html.DisplayNameFor(model => model.grad)
</dt>
<dd>
@Html.DisplayFor(model => model.grad)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.stuid }) |
@Html.ActionLink("Back to List", "Index")
</p>
改
@model Models.Student
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.id)
<div class="form-group">
@Html.LabelFor(model => model.stuid, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.stuid, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.stuid, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.age, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.age, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.age, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.sex, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.sex, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.sex, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.phone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Class, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Class, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Class, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.grad, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.grad, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.grad, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="保存" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
索引,展示全部信息
@model IEnumerable<Models.Student>
@{
ViewBag.Title = "Index";
}
<h2>@ViewBag.Title </h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.stuid)
</th>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor(model => model.age)
</th>
<th>
@Html.DisplayNameFor(model => model.sex)
</th>
<th>
@Html.DisplayNameFor(model => model.phone)
</th>
<th>
@Html.DisplayNameFor(model => model.Class)
</th>
<th>
@Html.DisplayNameFor(model => model.grad)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.stuid)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.age)
</td>
<td>
@Html.DisplayFor(modelItem => item.sex)
</td>
<td>
@Html.DisplayFor(modelItem => item.phone)
</td>
<td>
@Html.DisplayFor(modelItem => item.Class)
</td>
<td>
@Html.DisplayFor(modelItem => item.grad)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.id }) |
@Html.ActionLink("Details", "Details", new { id = item.id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.id })
</td>
</tr>
}
</table>
然后在web config里面添加具体内容
具体添加第二个add name 由于第一个已经生成,add name和sqlhelper里相一致
然后将MVC设为启动项目,进行测试