2.本例將頁面提交(POST)過來的資料繫結給資料表模型進行資料存放區
本例環境:ASP.NET 2.0,VS.NET 2005
//------------------------------------------
使用模型進行資料存放區
private string _PGID;
public string PGID
...{
get
...{
if(_PGID == null)
_PGID = Request.QueryString["PUID"];
return _PGID;
}
set
...{
_PGID = value;
}
}
//1.使用資料表模型進行快速的資料繫結與同步,實現有[Save21()]同樣的效果與效能
//
public void Save11()
...{
//BCF_FBT_Group code part:: declare PGID as key column
//
//[QColumn(Name = "PGID", Type = ColType.Text , Iskey = true)]
//public QColumn PGID;
BCF_FBT_Group g = new BCF_FBT_Group();
//1) 將WEB頁面提交過來的表單資料,綁定給Group的執行個體@g
//
g.Bind(Request.Form);
//2) 確定Key列的值
//
if(this.PGID == null)
this.PGID = Guid.NewGuid().ToString("N");
g.PGID.Value = this.PGID;
//3) 綁定後,執行同步,將資料同步到資料庫裡
//
g.Synchronize();
}
//2.比[Save11()] 效能更優的處理辦法,但是不知道資料庫裡是否真有存有一條記錄
//
public void Save12()
...{
BCF_FBT_Group g = new BCF_FBT_Group();
//other bind::
// g.bind(opsXml);
// g.bind(HashTable);
// g.bind(IPSData);
// g.bind(......);
g.Bind(Request.Form);
if(this.PGID == null) //沒有對資料庫是存真的存在資料進行判斷
...{
g.PGID.Value = Guid.NewGuid().ToString("N");
g.Insert();
}
else
...{
g.PGID.Value = this.PGID;
g.Update();
}
}
//------------------------------------------------------------------------
以下使用SDQRun實現同等效果
//1.使用SQRun 實現資料儲存(插入或更新)
//
public void Save21()
...{
SQRun SQ = new SQRun();
BCF_FBT_Group g = new BCF_FBT_Group();
g.Bind(Request.Form);
if(this.PGID == null)
this.PGID = Guid.NewGuid().ToString("N");
g.PGID.Value = this.PGID;
SQ.From(g);
SQ.Where(g.PGID == g.PGID.Value);
if(SQ.Exists()) //選判斷資料庫裡是否已存有記錄
SQ.Set(g).Update();//延用了上面的Where條件
else
SQ.To(g).Insert();
}
//2.使用SQRun 實現資料儲存(使用UnWhere只插入資料)
//
public void Save21_onlyinsert1()//與[Save21_onlyinsert2()]實現同樣同效能的效果
...{
SQRun SQ = new SQRun();
BCF_FBT_Group g = new BCF_FBT_Group();
g.Bind(Request.Form);
if(this.PGID == null)
this.PGID = Guid.NewGuid().ToString("N");
g.PGID.Value = this.PGID;
SQ.To(g).UnWhere(g.PGID == this.PGID).Insert(); //當不滿足該條件時,才插入資料
}
//---------------------------------------------
UnWhere 的實現原理::
//3.使用SQRun (UnWhere) 的實現原理
//
public void Save21_onlyinsert2()
...{
SQRun SQ = new SQRun();
BCF_FBT_Group g = new BCF_FBT_Group();
g.Bind(Request.Form);
if(this.PGID == null)
this.PGID = Guid.NewGuid().ToString("N");
g.PGID.Value = this.PGID;
SQ.From(g);//如果使用 UnWhere ,可以壓縮代碼量
SQ.Where(g.PGID == g.PGID.Value);
if(SQ.Exists() == false) //選判斷資料庫裡是否已存有記錄
SQ.To(g).Insert();
}