1、数据库中的新建一个表CSJ
2、分别创建五个文件
(1)、Controllers中添加【CsjController.cs】和View中【Index.cshtml】
CsjController.cs页面代码
using System;
using System.Collections.Generic;
using CityCard.Web.Admin.Model;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Drawing;
using System.Net; //LoginType 需要
namespace CityCard.Web.Admin.Controllers
{
public class CsjController : Controller
{
public ActionResult Index()
{
var Bll_User = new CityCard.Web.Admin.BLL.Csj();
ViewData["User"] = Bll_User.Get_FieldValue("关羽") ; // 获取姓名等于关羽的记录
return View();
}
}
}
Index.cshtml页面代码
@{
var user1 = ViewData["User"] as CityCard.Web.Admin.Model.Csj;
Layout = null; // 不显示默认加载的页面
}
--- 从数据表中读取一条记录中各字段的值 ---
编号:@Html.Raw(user1.ID)
姓名:@Html.Raw(user1.Name)
年龄:@Html.Raw(user1.Old)
(2)、BLL增加csj.cs(业务逻辑层,程序实现逻辑)
csj.cs页面代码
using System;
using System.Data;
using System.Collections.Generic;
using CityCard.Web.Admin.Model;
using System.Collections.Specialized;
namespace CityCard.Web.Admin.BLL
{
///
/// Sys_Employee
///
public partial class Csj
{
private readonly CityCard.Web.Admin.DAL.Csj dal = new CityCard.Web.Admin.DAL.Csj();
public Csj()
{ }
#region BasicMethod
public CityCard.Web.Admin.Model.Csj Get_FieldValue(string Code)
{
return dal.Get_FieldValue(Code); //
}
#endregion BasicMethod
}
}
(3)、Model增加Csj.cs(数据表类,映射数据库表结构)
csj.cs页面代码
using System;
using Dapper.Contrib.Extensions;
namespace CityCard.Web.Admin.Model
{
[Serializable]
[Table("[Csj]")]
public partial class Csj
{
public Csj()
{
}
#region Model
private int _id;
private string _name;
private int _old;
public int ID
{
set { _id = value; }
get { return _id; }
}
public string Name
{
set { _name = value; }
get { return _name; }
}
public int Old
{
set { _old = value; }
get { return _old; }
}
#endregion Model
}
}
(4)、DAL增加Csj.cs(数据库操作命令层)
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Specialized;
using CodeMagic.DBUtility;
using System.Linq;
using System.Collections.Generic;
using CityCard.Common.Extensions;//ToMD5需要
namespace CityCard.Web.Admin.DAL
{
///
/// 数据访问类:User
///
public partial class Csj
{
public Csj()
{ }
#region BasicMethod
public CityCard.Web.Admin.Model.Csj Get_FieldValue(string Code)
{
var query = "select * from Csj where Name=@Code";
return DbHelperSQL.Query(query, new { Code = Code }).FirstOrDefault();
}
#endregion
}
}
1、数据库中的新建一个表CSJ
2、分别创建五个文件
(1)、Controllers中添加【CsjController.cs】和View中【Index.cshtml】
CsjController.cs页面代码
using System;
using System.Collections.Generic;
using CityCard.Web.Admin.Model;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Drawing;
using System.Net; //LoginType 需要
namespace CityCard.Web.Admin.Controllers
{
public class CsjController : Controller
{
public ActionResult Index()
{
var Bll_User = new CityCard.Web.Admin.BLL.Csj();
ViewData["UserAll"] = Bll_User.GeTabletAll("1", "关羽"); // 获取表的所有记录
return View();
}
}
}
Index.cshtml页面代码
@{
//var user1 = ViewData["User"] as CityCard.Web.Admin.Model.Csj;
Layout = null; // 不显示默认加载的页面
var UserAll = ViewData["UserAll"] as List;
}
编号|姓名|年龄
@foreach (var item in UserAll)
{
@item.ID|@item.Name|@item.Old
}
(2)、BLL增加csj.cs(业务层)
csj.cs页面代码
using System;
using System.Data;
using System.Collections.Generic;
using CityCard.Web.Admin.Model;
using System.Collections.Specialized;
namespace CityCard.Web.Admin.BLL
{
///
/// Sys_Employee
///
public partial class Csj
{
private readonly CityCard.Web.Admin.DAL.Csj dal = new CityCard.Web.Admin.DAL.Csj();
public Csj()
{ }
#region BasicMethod
public List GeTabletAll(string Code, string m_name)
{
return dal.GeTabletAll(Code,m_name);
}
#endregion BasicMethod
}
}
(3)、Model增加Csj.cs(数据表结构)
csj.cs页面代码
using System;
using Dapper.Contrib.Extensions;
namespace CityCard.Web.Admin.Model
{
[Serializable]
[Table("[Csj]")]
public partial class Csj
{
public Csj()
{
}
#region Model
private int _id;
private string _name;
private int _old;
public int ID
{
set { _id = value; }
get { return _id; }
}
public string Name
{
set { _name = value; }
get { return _name; }
}
public int Old
{
set { _old = value; }
get { return _old; }
}
#endregion Model
}
}
(4)、DAL增加Csj.cs(数据库操作命令层)
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Specialized;
using CodeMagic.DBUtility;
using System.Linq;
using System.Collections.Generic;
using CityCard.Common.Extensions;//ToMD5需要
namespace CityCard.Web.Admin.DAL
{
///
/// 数据访问类:User
///
public partial class Csj
{
public Csj()
{ }
#region BasicMethod
public List GeTabletAll(string Code,string m_name)
{
var query = "select * from Csj where Id>@Code and Name=@m_name";
return DbHelperSQL.Query(query, new { Code = Code, m_name= m_name }).ToList();
}
#endregion
}
}