日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SubSonic中的字段付值--MakeOld Update

發布時間:2023/12/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SubSonic中的字段付值--MakeOld Update 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? 根據設計當MakeOld后(在讀取數據庫后,或者手動調用),對記錄(SubSonic生成的類)屬性附值時,Sonic會檢測這個Value是否與原來的不同,只有值不同時才會附值成功,并將該列添加到DirtyColumns,而DirtyColumns中的列才會被Update采用,一般情況下 只要所有列中有一個列的是Dirty==true(被更改過),那么在Save時就會采用Update,

注意:SubSonic中判斷是否采用Update判斷“全部字段集合”中是否存在一個字段被更改,而生成Update命令時使用的集合是DirtyColumns集合,而不是直接從全部字段集合中查找那些被更改的字段,在多數情況下不會出問題,但有些時候可能帶來意想不到的問題,參考下面的場境3。

//=============參考代碼1================

??????????? public bool IsDirty
??????????? {
??????????????? get
??????????????? {
??????????????????? foreach(TableColumnSetting setting in this)
??????????????????? {
??????????????????????? if(setting.IsDirty)
??????????????????????????? return true;
??????????????????? }
??????????????????? return false;
??????????????? }
??????????????? //set
??????????????? //{
??????????????? //??? foreach (TableColumnSetting setting in this)
??????????????? //??????? setting.IsDirty = value;
??????????????? //}
??????????? }

//============參考代碼2============

???? public QueryCommand GetSaveCommand(string userName)
??????? {
??????????? if(IsNew)
??????????????? return GetInsertCommand(userName);

??????????? if(IsDirty)
??????????????? return GetUpdateCommand(userName);

??????????? return null;
??????? }

如果您對記錄未做任何更改而直接調用?.Save()時會根據IsNew與IsDirty的取值來決定采用Insert或Update。

如果兩個多是false,那么就返回null.那么Save操作將什么都不做。

//============參考代碼3============

?? QueryCommand cmd = GetSaveCommand(userName);
??????????????? if(cmd == null)
??????????????????? return;

場境1:

Employe emp=new Employe(1);//加載一個員工數據

//這個時候 IsNew=false,IsDirty=false

emp.Name=emp.Name; //沒有改變

emp.Save();//這里的Save方法將不做任何處理

?

場境2:

Employe emp=new Employe(1);

emp.Name=txtName.txt;//假設您在文本框中調整了Name取值

//這個時候 isNew=false;IsDirty=true;

emp.Save();將使用Update,并且只更新Name字段。

?

場境3:(報錯)

Employe emp=new Employe(1);

Employe backEmp=new Employe();

backEmp.CopyFrom(backEmp);

//copyFrom后會backEmp.IsDirty全部是true

//參考代碼7

backEmp.Name="xxxx";

backEmp.MackOld();//將IsNew設置成False

backEmp.Save(); //報錯,IsDirty=True,而DirtyColumns為空,

//生成 Update Employe Set Where EmpoyeId=1 這樣的錯誤TSQL

?

場境4:(報錯)

Employe emp=new Employe(1);

Employe backEmp=new Employe();

backEmp.CopyFrom(backEmp);

backEmp.MackOld();

backEmp.Name=backEmp.Name;

backEmp.Save();//這時IsDirty是True,而DirtyColumns為空

?

正確作法,應該在backEmp.MackOld();后再調用backEmp.MackClear();

//=============參考代碼8=================

????? /// <summary>
??????? /// Called after any property is set. Sets IsDirty to <c>true</c>.
??????? /// </summary>
??????? public void MarkClean()
??????? {
??????????? foreach(TableSchema.TableColumnSetting setting in columnSettings)
??????????????? setting.IsDirty = false;
??????????? DirtyColumns.Clear();
??????? }

//========參考代碼4==========

??????? /// <summary>
??????? /// Called after Update() invokation. Sets IsNew to <c>false</c>.
??????? /// </summary>
??????? public void MarkOld()
??????? {
??????????? IsLoaded = true;
??????????? _isNew = false;
??????? }

//========參考代碼5======================

??????? /// <summary>
??????? /// Copies the passed-in instance settings to this instance
??????? /// </summary>
??????? /// <param name="copyInstance">The copy instance.</param>
??????? public void CopyFrom(T copyInstance)
??????? {
??????????? if(copyInstance == null)
??????????????? throw new ArgumentNullException("copyInstance");

??????????? foreach(TableSchema.TableColumnSetting setting in copyInstance.columnSettings)
??????????????? SetColumnValue(setting.ColumnName, setting.CurrentValue);
??????? }

//========參考代碼6======================

??????? /// <summary>
??????? /// Sets a value for a particular column in the record
??????? /// </summary>
??????? /// <param name="columnName">Name of the column, as defined in the database</param>
??????? /// <param name="oValue">The value to set the type to</param>
??????? public void SetColumnValue(string columnName, object oValue)
??????? {
??????????? columnSettings = columnSettings ?? new TableSchema.TableColumnSettingCollection();

??????????? // add the column to the DirtyColumns
??????????? // if this instance has already been loaded
??????????? // and this is a change to existing values
??????????? if(IsLoaded && !IsNew)
??????????? {
??????????????? TableSchema.Table schema = GetSchema();
??????????????? object oldValue = null;
??????????????? string oldValueMsg = "NULL";
??????????????? string newValueMsg = "NULL";
??????????????? bool areEqualOrBothNull = false;

??????????????? try
??????????????? {
??????????????????? oldValue = columnSettings.GetValue(columnName);
??????????????? }
??????????????? catch {}

??????????????? if(oldValue == null && oValue == null)
??????????????????? areEqualOrBothNull = true;
??????????????? else
??????????????? {
??????????????????? if(oldValue != null)
??????????????????? {
??????????????????????? oldValueMsg = oldValue.ToString();
??????????????????????? areEqualOrBothNull = oldValue.Equals(oValue);
??????????????????? }

??????????????????? if(oValue != null)
??????????????????????? newValueMsg = oValue.ToString();
??????????????? }

??????????????? TableSchema.TableColumn dirtyCol = schema.GetColumn(columnName);

??????????????? if(dirtyCol != null && !areEqualOrBothNull)
??????????????? {
??????????????????? string auditMessage = String.Format("Value changed from {0} to {1}{2}", oldValueMsg, newValueMsg, Environment.NewLine);
??????????????????? TableSchema.TableColumn dirtyEntry = DirtyColumns.GetColumn(columnName);
??????????????????? if(dirtyEntry != null)
??????????????????? {
??????????????????????? DirtyColumns.Remove(dirtyEntry);
??????????????????????? auditMessage = String.Concat(dirtyCol.AuditMessage, auditMessage);
??????????????????? }

??????????????????? dirtyCol.AuditMessage = auditMessage;
??????????????????? DirtyColumns.Add(dirtyCol);
??????????????? }
??????????? }

??????????? columnSettings.SetValue(columnName, oValue);//這里最終調用參考代碼7


??????? }

//============參考代碼7============

??????????? /// <summary>
??????????? /// Gets or sets the current value.
??????????? /// </summary>
??????????? /// <value>The current value.</value>
??????????? public object CurrentValue
??????????? {
??????????????? get { return _currentValue; }
??????????????? set
??????????????? {
??????????????????? if(value == null && _currentValue == null)
??????????????????????? return;

??????????????????? if(value != null)
??????????????????? {
??????????????????????? if(value.Equals(_currentValue))
??????????????????????????? return;
??????????????????? }

??????????????????? _currentValue = value;
??????????????????? _isDirty = true;
??????????????? }
??????????? }

轉載于:https://www.cnblogs.com/wdfrog/archive/2010/06/30/1768159.html

總結

以上是生活随笔為你收集整理的SubSonic中的字段付值--MakeOld Update的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。