/// <summary> /// 構造函數 /// </summary> /// <param name="mConn">SQLHelper對象</param> /// <param name="SQL">SQL語句</param> /// <param name="OptionType">如何操作</param> /// <param name="TableName">Table 別名</param> internal RecordSet(SQLHelper mConn, string SQL, tagUpdateMode OptionType, string TableName) { string mySQL = SQL; if (SQL.IndexOf(' ') < 0) { if (OptionType == tagUpdateMode.RT_Insert) mySQL = "select * from " + mySQL + " where 1=0 "; else mySQL = "select * from " + mySQL; } this.conn = mConn; this._ICursor = this.conn.OpenDataTableForUpdate(mySQL, out myDataAdapter, TableName); this.myDataAdapter.RowUpdated += new SqlRowUpdatedEventHandler(myDataAdapter_RowUpdated); this.myDataAdapter.RowUpdating += new SqlRowUpdatingEventHandler(myDataAdapter_RowUpdating); this.mEditMode = tagUpdateMode.RT_Browse; }
/// <summary> /// 提交當前行的修改到數據庫 /// </summary> /// <param name="UserID">用戶ID</param> public void Update(string UserID) { if (this.Postion < 0) { throw new Exception("this table is BOF!!!!"); } if (this.Postion >= this._ICursor.Rows.Count) { throw new Exception("this table is EOF!!!!"); } switch (this.mEditMode) { case tagUpdateMode.RT_Browse: throw new Exception("Please run PreInsert() or PreUpdate() firstly!"); case tagUpdateMode.RT_Insert: this._ICursor.Rows[this.Postion]["ie_ymd"] = string.Format("{0:yyyy/MM/dd}", DateTime.Now).Replace('-', '/'); this._ICursor.Rows[this.Postion]["ie_time"] = string.Format("{0:HH:mm:ss}", DateTime.Now); this._ICursor.Rows[this.Postion]["ie_user"] = UserID; this._ICursor.Rows[this.Postion].EndEdit(); this.myDataAdapter.Update(this._ICursor); this._ICursor.AcceptChanges(); break; case tagUpdateMode.RT_Update: this._ICursor.Rows[this.Postion]["ie_lymd"] = string.Format("{0:yyyy/MM/dd}", DateTime.Now).Replace('-', '/'); this._ICursor.Rows[this.Postion]["ie_ltime"] = string.Format("{0:HH:mm:ss}", DateTime.Now); this._ICursor.Rows[this.Postion]["ie_luser"] = UserID; this._ICursor.Rows[this.Postion].EndEdit(); this.myDataAdapter.Update(this._ICursor.GetChanges()); this._ICursor.AcceptChanges(); break; } this.mEditMode = tagUpdateMode.RT_Browse; return; }
/// <summary> /// 修改當前記錄 /// 請先執行Read()方法 /// </summary> public void PreUpdate() { if (this.Postion < 0) { throw new Exception("No record exist in the Table, please run the Read() method firstly!"); } this.mEditMode = tagUpdateMode.RT_Update; this._ICursor.Rows[this.Postion].BeginEdit(); }
/// <summary> /// 新增一筆記錄 /// </summary> public void PreInsert() { this.mEditMode = tagUpdateMode.RT_Insert; this._ICursor.Rows.Add(this._ICursor.NewRow()); this.Postion = _ICursor.Rows.Count - 1; this._ICursor.Rows[Postion].BeginEdit(); }
/// <summary> /// 打開需要修改的記錄集 /// </summary> /// <param name="SQL">SQL語句(請使用select * )</param> /// <param name="OptionType">編輯類別</param> /// <param name="TableName">指定表別名</param> /// <returns></returns> private RecordSet OpenDataTableForUpdate(string SQL, tagUpdateMode OptionType, string TableName) { return new RecordSet(this, SQL, OptionType, TableName); }
/// <summary> /// 對指定表的記錄進行修改 /// </summary> /// <param name="TableName">表名</param> /// <param name="UID">uid的值</param> /// <param name="OptionType">修改模式</param> /// <returns></returns> public RecordSet OpenDataTable(string TableName, string UID, tagUpdateMode OptionType) { return this.OpenDataTableForUpdate("select * from " + TableName + " where uid = " + UID, OptionType, ""); }
/// <summary> /// 對指定表新增記錄 /// </summary> /// <param name="TableName">表名</param> /// <param name="OptionType">編輯類別</param> /// <returns></returns> public RecordSet OpenDataTable(string TableName, tagUpdateMode OptionType) { return this.OpenDataTableForUpdate(TableName, OptionType, ""); }