DEMO 数据表: [PhotoCategory](照片分类表) ID(int) 类别ID Name(nvarchar(50)) 类别名称 [Photo](照片表) ID(int) 照片ID Title(nvarchar(200)) 照片标题 URL(varchar(200)) 照片HTTP地址 CategoryID(int) 类别ID Descript(ntext) 照片描述信息 AddDate(datetime) 照片的添加时间 定义对应数据表的实体类
namespace Entities{//对应数据表[Photo][Table(TableName = "Photo")]public class Photo{//标识此字段为唯一标识列(identity)[Field(IsId = true)]public int ID{get;set;}public string Title{get;set;}public string URL{get;set;}public int CategoryID{get;set;}public string Descript{get;set;}//标识此字段在修改的时候忽略此字段//orm最终生成的 sql, 不会对此字段进行修改 //最终sql:update photo set title =@title,url=@url,categoryid=@categoryid,descript=@descript where id=@id[Field(AllowModifyUndefined = true)]public DateTime AddDate{get;set;}}}
添加照片页面(表单元素的 name 属性数据库字段名对应起来名字一样) PhotoAdd.aspx
<html><head><title>添加照片</title></head><body><form action="?action=save" method="post"><lable>照片标题</lable><input type="text" name="Title" /><lable>照片HTTP地址</lable><input type="text" name="URL" /><lable>照片类别</lable><select name="CategoryID"><% var categoryList = DbHelper.Instance.GetDataList<Entities.PhotoCategory>(100,null,null); if(categoryList!=null) { foreach(var item in categoryList) { %> <option value="<%=item.ID%>"><%=item.Name%></option> <% } }%></select><lable>照片描述</lable><input type="text" name="Descript" /><input type="submit" value="保存" /></form></body></html>
PhotoAdd.aspx.cs
using System;public partial class m_Album : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { string action = Request.QueryString["action"] ?? string.Empty; if(action=="save") { Save(); } } void Save() { var ent = RequestHelper.Get<Entities.Photo>();//从http get post 的数据中获得一个Entities.Photo 实例 ent.AddDate=DateTime.Now; int id = DbHelper.Instance.InsertDataAndGetID<Entities.Photo, int>(ent);//把实例保存到数据库 ,并返回它的标识 }}
<无> .CodeEntity .code_pieces ul.piece_anchor{width:25px;position:absolute;top:25px;left:-30px;z-index:1000;}.CodeEntity .code_pieces ul.piece_anchor li{width:25px;background: #efe;margin-bottom:2px;}.CodeEntity .code_pieces ul.piece_anchor li{border-left:3px #40AA63 solid;border-right:3px #efe solid;}.CodeEntity .code_pieces ul.piece_anchor li:hover{border-right:3px #40AA63 solid;border-left:3px #efe solid;}.CodeEntity .code_pieces ul.piece_anchor li a{color: #333;padding: 3px 10px;}.CodeEntity .code_pieces .jump_to_code{visibility:hidden;position:relative;}.CodeEntity .code_pieces .code_piece:hover .jump_to_code{visibility:visible;}.CodeEntity .code_pieces .code_piece:hover .jump_to_code a{text-decoration:none;}.CodeEntity .code_pieces h2 i{float:right;font-style:normal;font-weight:normal;}.CodeEntity .code_pieces h2 i a{font-size:9pt;background: #FFFFFF;color:#00A;padding: 2px 5px;text-decoration:none;}