数据库操作层:DBL
业务逻辑层:BLL
模块:Module
表现层:.aspx.cs ,.aspx
这里用了一点点反射的东西来向实体类显示东东
DataBaseFactory.cs
![]() ![]() | using System; | |
![]() ![]() | using System.Collections.Generic; | |
![]() ![]() | using System.Text; | |
![]() ![]() | using System.Data; | |
![]() ![]() | using System.Data.Common; | |
![]() ![]() | using System.Data.SqlClient; | |
![]() ![]() | using System.Configuration; | |
![]() ![]() | using System.Reflection; | |
![]() ![]() | ||
![]() ![]() | namespace DBL | |
![]() ![]() | { | |
![]() ![]() | public class DataBaseFactory | |
![]() ![]() | { | |
![]() ![]() | public static IDataReader GetByDataReader(string sql) | |
![]() ![]() | { | |
![]() ![]() | SqlConnection conn = new SqlConnection(); | |
![]() ![]() | conn.ConnectionString = ConfigurationManager.ConnectionStrings["ExamCenterConnectionString"].ConnectionString; | |
![]() ![]() | conn.Open(); | |
![]() ![]() | SqlCommand cmd = new SqlCommand(sql, conn); | |
![]() ![]() | return cmd.ExecuteReader(CommandBehavior.CloseConnection); | |
![]() ![]() | } | |
![]() ![]() | ||
![]() ![]() | public static object PopulateEntity(DataColumnMapping[] mappings, IDataReader reader, object entity) | |
![]() ![]() | { | |
![]() ![]() | foreach (DataColumnMapping mapping in mappings) | |
![]() ![]() | { | |
![]() ![]() | int ordinalPosition = -1; | |
![]() ![]() | object propertyValue = DBNull.Value; | |
![]() ![]() | try | |
![]() ![]() | { | |
![]() ![]() | ordinalPosition = reader.GetOrdinal(mapping.SourceColumn); | |
![]() ![]() | propertyValue = reader.GetValue(ordinalPosition); | |
![]() ![]() | } | |
![]() ![]() | catch (IndexOutOfRangeException ex) | |
![]() ![]() | { | |
![]() ![]() | //throw new PropertyColumnMappingException(mapping.SourceColumn + " is not a valid SourceColumn", ex); | |
![]() ![]() | } | |
![]() ![]() | ||
![]() ![]() | if (propertyValue != DBNull.Value) | |
![]() ![]() | { | |
![]() ![]() | if (mapping.DataSetColumn == "ID") | |
![]() ![]() | { | |
![]() ![]() | Nullable<int> tempValue = (int)propertyValue; | |
![]() ![]() | propertyValue = tempValue; | |
![]() ![]() | } | |
![]() ![]() | object[] param = { propertyValue }; | |
![]() ![]() | try | |
![]() ![]() | { | |
![]() ![]() | entity.GetType().InvokeMember(mapping.DataSetColumn, BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Static, null, entity, param); | |
![]() ![]() | } | |
![]() ![]() | catch (Exception e) | |
![]() ![]() | { | |
![]() ![]() | //throw new PropertyColumnMappingException(GetPropertyColumnMappingExceptionMessage(mapping), e); | |
![]() ![]() | } | |
![]() ![]() | } | |
![]() ![]() | } | |
![]() ![]() | return entity; | |
![]() ![]() | } | |
![]() ![]() | } | |
![]() ![]() | } | |
![]() ![]() |
GroupInfo.cs
![]() ![]() | using System; | |
![]() ![]() | using System.Collections.Generic; | |
![]() ![]() | using System.Text; | |
![]() ![]() | using System.Data.Common; | |
![]() ![]() | ||
![]() ![]() | namespace Module | |
![]() ![]() | { | |
![]() ![]() | [Serializable] | |
![]() ![]() | public class GroupInfo | |
![]() ![]() | { | |
![]() ![]() | private int _GroupID; | |
![]() ![]() | public int uGroupID | |
![]() ![]() | { | |
![]() ![]() | get { return _GroupID; } | |
![]() ![]() | set { _GroupID = value; } | |
![]() ![]() | } | |
![]() ![]() | private string _GroupName; | |
![]() ![]() | public string uGroupName | |
![]() ![]() | { | |
![]() ![]() | get { return _GroupName; } | |
![]() ![]() | set { _GroupName = value; } | |
![]() ![]() | } | |
![]() ![]() | ||
![]() ![]() | | |
![]() ![]() | | |
![]() ![]() | } | |
![]() ![]() | } | |
![]() ![]() |
Group.cs
![]() ![]() | using System; | |
![]() ![]() | using System.Collections.Generic; | |
![]() ![]() | using System.Data; | |
![]() ![]() | using System.Data.Common; | |
![]() ![]() | using System.Text; | |
![]() ![]() | using Module; | |
![]() ![]() | using DBL; | |
![]() ![]() | ||
![]() ![]() | namespace BLL | |
![]() ![]() | { | |
![]() ![]() | public class Group | |
![]() ![]() | { | |
![]() ![]() | private DataColumnMapping[] mappings = new DataColumnMapping[] { new DataColumnMapping("jd_uGroupID", "uGroupID"), new DataColumnMapping("jd_uGroupName", "uGroupName") }; | |
![]() ![]() | public GroupInfo GetInfoByID(int GroupID) | |
![]() ![]() | { | |
![]() ![]() | GroupInfo gi=new GroupInfo(); | |
![]() ![]() | IDataReader dr = DataBaseFactory.GetByDataReader("select * from jd_uGroup where jd_uGroupID=3"); | |
![]() ![]() | if (dr.Read()) | |
![]() ![]() | { | |
![]() ![]() | gi = (GroupInfo)DataBaseFactory.PopulateEntity(mappings, dr, gi); | |
![]() ![]() | } | |
![]() ![]() | return gi; | |
![]() ![]() | } | |
![]() ![]() | public IList<GroupInfo> GetListInfo() | |
![]() ![]() | { | |
![]() ![]() | IList<GroupInfo> gis = new List<GroupInfo>(); | |
![]() ![]() | IDataReader dr = DataBaseFactory.GetByDataReader("select * from jd_Exam"); | |
![]() ![]() | while (dr.Read()) | |
![]() ![]() | { | |
![]() ![]() | GroupInfo gi = new GroupInfo(); | |
![]() ![]() | gi = (GroupInfo)DataBaseFactory.PopulateEntity(mappings, dr, gi); | |
![]() ![]() | gis.Add(gi); | |
![]() ![]() | } | |
![]() ![]() | return gis; | |
![]() ![]() | } | |
![]() ![]() | | |
![]() ![]() | } | |
![]() ![]() | } | |
![]() ![]() |
Default.aspx.cs
![]() ![]() | using System; | |
![]() ![]() | using System.Data; | |
![]() ![]() | using System.Configuration; | |
![]() ![]() | using System.Collections; | |
![]() ![]() | using System.Web; | |
![]() ![]() | using System.Web.Security; | |
![]() ![]() | using System.Web.UI; | |
![]() ![]() | using System.Web.UI.WebControls; | |
![]() ![]() | using System.Web.UI.WebControls.WebParts; | |
![]() ![]() | using System.Web.UI.HtmlControls; | |
![]() ![]() | ||
![]() ![]() | ||
![]() ![]() | namespace TestDB | |
![]() ![]() | { | |
![]() ![]() | public partial class _Default : System.Web.UI.Page | |
![]() ![]() | { | |
![]() ![]() | protected void Page_Load(object sender, EventArgs e) | |
![]() ![]() | { | |
![]() ![]() | | |
![]() ![]() | } | |
![]() ![]() | ||
![]() ![]() | protected void Button1_Click(object sender, EventArgs e) | |
![]() ![]() | { | |
![]() ![]() | BLL.Group g = new BLL.Group(); | |
![]() ![]() | Button1.Text = g.GetInfoByID(3).uGroupName; | |
![]() ![]() | GridView1.DataSource = g.GetListInfo(); | |
![]() ![]() | GridView1.DataBind(); | |
![]() ![]() | } | |
![]() ![]() | } | |
![]() ![]() | } | |
![]() ![]() |
源码下载:
你的下载权限 0 低于此附件所需权限 10, 你无法查看此附件
注:需注册用户才能下载










操作提示