private void SetDataRowWithLoadOption (DataRow dataRow, int recordNo, LoadOption loadOption, bool checkReadOnly) { bool hasError = false; if (checkReadOnly) { foreach(DataColumn dc in this.Columns) { if (dc.ReadOnly && !dc.Computed) { switch(loadOption) { case LoadOption.OverwriteChanges: if ((dataRow[dc, DataRowVersion.Current] != dc[recordNo]) ||(dataRow[dc, DataRowVersion.Original] != dc[recordNo])) hasError = true; break; case LoadOption.Upsert: if (dataRow[dc, DataRowVersion.Current] != dc[recordNo]) hasError = true; break; case LoadOption.PreserveChanges: if (dataRow[dc, DataRowVersion.Original] != dc[recordNo]) hasError = true; break; } } } } // No Event should be fired in SenNewRecord and SetOldRecord // fire rowChanging event here DataRowChangeEventArgs drcevent = null; DataRowAction action = DataRowAction.Nothing; int cacheTempRecord = dataRow.tempRecord; dataRow.tempRecord = recordNo; switch(loadOption) { case LoadOption.OverwriteChanges: action = DataRowAction.ChangeCurrentAndOriginal; break; case LoadOption.Upsert: switch(dataRow.RowState) { case DataRowState.Unchanged: // let see if the incomming value has the same values as existing row, so compare records foreach(DataColumn dc in dataRow.Table.Columns) { if (0 != dc.Compare(dataRow.newRecord, recordNo)) { action = DataRowAction.Change; break; } } break; case DataRowState.Deleted: Debug.Assert(false, "LoadOption.Upsert with deleted row, should not be here"); break; default : action = DataRowAction.Change; break; } break; case LoadOption.PreserveChanges: switch(dataRow.RowState) { case DataRowState.Unchanged: action = DataRowAction.ChangeCurrentAndOriginal; break; default: action = DataRowAction.ChangeOriginal; break; } break; default: throw ExceptionBuilder.ArgumentOutOfRange("LoadOption"); } try { drcevent = RaiseRowChanging(null, dataRow, action); if (action == DataRowAction.Nothing) { // RaiseRowChanging does not fire for DataRowAction.Nothing dataRow.inChangingEvent = true; try { drcevent = OnRowChanging(drcevent, dataRow, action); } finally { dataRow.inChangingEvent = false; } } } finally { Debug.Assert(dataRow.tempRecord == recordNo, "tempRecord has been changed in event handler"); if (DataRowState.Detached == dataRow.RowState) { // 'row.Table.Remove(row);' if (-1 != cacheTempRecord) { FreeRecord(ref cacheTempRecord); } } else { if (dataRow.tempRecord != recordNo) { // 'row.EndEdit(); row.BeginEdit(); ' if (-1 != cacheTempRecord) { FreeRecord(ref cacheTempRecord); } if (-1 != recordNo) { FreeRecord(ref recordNo); } recordNo = dataRow.tempRecord; } else { dataRow.tempRecord = cacheTempRecord; } } } if (dataRow.tempRecord != -1) { dataRow.CancelEdit(); } switch(loadOption) { case LoadOption.OverwriteChanges: this.SetNewRecord(dataRow, recordNo, DataRowAction.Change, false, false); this.SetOldRecord(dataRow, recordNo); break; case LoadOption.Upsert: if (dataRow.RowState == DataRowState.Unchanged) { this.SetNewRecord(dataRow, recordNo, DataRowAction.Change, false, false); if (!dataRow.HasChanges()) { this.SetOldRecord(dataRow, recordNo); } } else { if (dataRow.RowState == DataRowState.Deleted) dataRow.RejectChanges(); this.SetNewRecord(dataRow, recordNo, DataRowAction.Change, false, false); } break; case LoadOption.PreserveChanges: if (dataRow.RowState == DataRowState.Unchanged) { // SQLBU 500706: DataTable internal index is corrupted: '8' // if ListChanged event deletes dataRow this.SetOldRecord(dataRow, recordNo); // do not fire event this.SetNewRecord(dataRow, recordNo, DataRowAction.Change, false, false); } else { // if modified/ added / deleted we want this operation to fire event (just for LoadOption.PreserveCurrentValues) this.SetOldRecord(dataRow, recordNo); } break; default: throw ExceptionBuilder.ArgumentOutOfRange("LoadOption"); } if (hasError) { string error = Res.GetString(Res.Load_ReadOnlyDataModified); if (dataRow.RowError.Length == 0) { // WebData 112272, append the row error dataRow.RowError = error; } else { dataRow.RowError += " ]:[ " + error ; } foreach(DataColumn dc in this.Columns) { if (dc.ReadOnly && !dc.Computed) dataRow.SetColumnError(dc, error); } } drcevent = RaiseRowChanged(drcevent, dataRow, action); if (action == DataRowAction.Nothing) { // RaiseRowChanged does not fire for DataRowAction.Nothing dataRow.inChangingEvent = true; try { OnRowChanged(drcevent, dataRow, action); } finally { dataRow.inChangingEvent = false; } } }
internal void GetEntityFromDataRow(DataColumnCollection columnCollection, DataRow row, IOfflineEntity objectToConvert) { Type t = objectToConvert.GetType(); Dictionary<string, string> mappingInfo = _localToGlobalPropertyMapping[t]; bool isDeleted = false; if (row.RowState == DataRowState.Deleted) { isDeleted = true; row.RejectChanges(); } // Note: Call BeginEdit only after check for Deleted row state, // otherwise this call will crash. row.BeginEdit(); for (Int32 i = 0; i <= columnCollection.Count - 1; i++) { if (IsSyncSpecificColumn(columnCollection[i].ColumnName)) { continue; } //NOTE: the datarow column names must match exactly (including case) to the IOfflineEntity's property names object columnValue = row[columnCollection[i].ColumnName]; if (DBNull.Value != columnValue) { t.InvokeMember((mappingInfo.ContainsKey(columnCollection[i].ColumnName)) ? mappingInfo[columnCollection[i].ColumnName] : columnCollection[i].ColumnName, BindingFlags.SetProperty, null, objectToConvert, new[] {columnValue}); } } if (isDeleted) { row.Delete(); // Mark the IsTombstone field if the RowState was deleted. objectToConvert.ServiceMetadata.IsTombstone = true; } row.EndEdit(); }
/// <summary> /// This method will try to locate the passed in DataRow's key values in the destination DataTable /// and if found will update its values with the new values passed. /// </summary> /// <param name="destinationTable">Table to make the merge in</param> /// <param name="rowToMatch">Row whose keys to match in destination table</param> /// <param name="rowvalues">Values to override</param> /// <param name="entityType">Entity type for the DataRow</param> internal void MergeChangeInToDataSet(DataTable destinationTable, DataRow rowToMatch, object[] rowvalues, Type entityType) { object[] primaryKeyColumns = new object[destinationTable.PrimaryKey.Length]; int index = 0; bool isRowToMatchDeleted = false; if (rowToMatch.RowState == DataRowState.Deleted) { isRowToMatchDeleted = true; rowToMatch.RejectChanges(); } // Find all PrimaryKey column indexes foreach (DataColumn pkColumn in destinationTable.PrimaryKey) { primaryKeyColumns[index++] = rowToMatch[pkColumn.ColumnName]; } // Find the row in DestinationTable DataRow rowToModify = destinationTable.Rows.Find(primaryKeyColumns); // Check for tombstones bool isRowDeleted = false; if (rowToModify.RowState == DataRowState.Deleted) { isRowDeleted = true; rowToModify.RejectChanges(); } // Suppress DataRow RowChanging events // Note: Call BeginEdit only after check for Deleted row state, // otherwise this call will crash. rowToModify.BeginEdit(); Debug.Assert(rowToModify != null); rowToModify.ItemArray = rowvalues; // Reset rowstates if (isRowDeleted) { rowToModify.Delete(); } if (isRowToMatchDeleted) { rowToMatch.Delete(); } rowToModify.EndEdit(); }
/// <summary> /// 删除行(根据表名称,以及行内容) /// </summary> /// <param name="tableName"></param> /// <param name="dr"></param> /// <returns></returns> public static int RemoveDataRow(string tableName,DataRow dr) { if (tableName.Trim().Equals(string.Empty)|| dr==null) return 0; int result = 0; using (InterfaceDataBase db = DataBaseManager.GetDataBase()) { try { //string strSqlOra = "select * from @TABLE_NAME "; //Dictionary<string, object> parameter = new Dictionary<string, object>(); //parameter.Add("TABLE_NAME", tableName); //dtLogResult = db.RunSqlQuery(strSqlOra, parameter); string strSql = "delete from " + tableName +" where 1=1 and "; if (dr.RowState == DataRowState.Deleted) { dr.RejectChanges(); } Dictionary<string, object> parameter = new Dictionary<string, object>(); int count = dr.Table.Columns.Count; for (int i = 0; i < count; i++) { string colNameNow = dr.Table.Columns[i].ColumnName.Trim(); object colValue = dr[i]; if (colValue == System.DBNull.Value) { strSql = strSql + dr.Table.Columns[i].ColumnName + " is null " ; } else { strSql = strSql + dr.Table.Columns[i].ColumnName + " =@" + dr.Table.Columns[i].ColumnName; parameter.Add(colNameNow, colValue); } if (i != count - 1) { strSql = strSql + " and "; } } result = db.RunSqlNoneQuery(strSql,parameter); } catch (Exception ex) { throw ex; } } return result; }
internal void Push(CUQueue UQueue, DataRow dr) { int n; bool bNull; if (dr == null) throw new ArgumentNullException("Datarow object can't be null"); if (m_dts == null) throw new ArgumentNullException("DataTable header is not serialized yet"); if (m_dts.Length != dr.ItemArray.Length) throw new InvalidOperationException("The size of the input data type array does not match the size of data row"); byte b = 0; byte bOne = 1; m_qBit.SetSize(0); m_qTemp.SetSize(0); bool bDelete = (dr.RowState == DataRowState.Deleted); if (bDelete) dr.RejectChanges(); object[] data = dr.ItemArray; int nLen = m_dts.Length; for (n = 0; n < nLen; n++) { object myData = data[n]; bNull = (myData == null || myData.Equals(DBNull.Value)); if (bNull) { b += (byte)(bOne << (byte)(n % 8)); } if ((n % 8) == 7) { m_qBit.Save(b); b = 0; } if (bNull) continue; switch (m_dts[n]) { case tagDataTypeSupported.dtBoolean: m_qTemp.Save((bool)myData); break; case tagDataTypeSupported.dtByte: m_qTemp.Save((byte)myData); break; case tagDataTypeSupported.dtChar: m_qTemp.Save((char)myData); break; case tagDataTypeSupported.dtDateTime: m_qTemp.Save((DateTime)myData); break; case tagDataTypeSupported.dtDecimal: m_qTemp.Save((decimal)myData); break; case tagDataTypeSupported.dtDouble: m_qTemp.Save((double)myData); break; case tagDataTypeSupported.dtFloat: m_qTemp.Save((float)myData); break; case tagDataTypeSupported.dtGuid: m_qTemp.Save((Guid)myData); break; case tagDataTypeSupported.dtUInt16: m_qTemp.Save((ushort)myData); break; case tagDataTypeSupported.dtUInt32: m_qTemp.Save((uint)myData); break; case tagDataTypeSupported.dtUInt64: m_qTemp.Save((ulong)myData); break; case tagDataTypeSupported.dtInt16: m_qTemp.Save((short)myData); break; case tagDataTypeSupported.dtInt32: m_qTemp.Save((int)myData); break; case tagDataTypeSupported.dtInt64: m_qTemp.Save((long)myData); break; case tagDataTypeSupported.dtString: m_qTemp.Save((string)myData); break; case tagDataTypeSupported.dtValue: case tagDataTypeSupported.dtValues: case tagDataTypeSupported.dtChars: case tagDataTypeSupported.dtBytes: case tagDataTypeSupported.dtTimeSpan: m_qTemp.Save(myData, false, false); break; case tagDataTypeSupported.dtUDT: m_qTemp.Save(myData.ToString()); break; default: throw new InvalidOperationException("Unsupported data type for serialization"); } } if ((n % 8) != 0) m_qBit.Save(b); UQueue.Push(m_qBit.m_bytes, m_qBit.GetSize()); UQueue.Push(m_qTemp.m_bytes, m_qTemp.GetSize()); if (bDelete) dr.Delete(); UQueue.Save((byte)dr.RowState); UQueue.Save(dr.HasErrors); if (dr.HasErrors) UQueue.Save(dr.RowError); }
private void SetDataRowWithLoadOption(DataRow dataRow, int recordNo, LoadOption loadOption, bool checkReadOnly) { bool flag = false; if (checkReadOnly) { foreach (DataColumn column in this.Columns) { if (column.ReadOnly && !column.Computed) { switch (loadOption) { case LoadOption.OverwriteChanges: goto Label_0058; case LoadOption.PreserveChanges: if (dataRow[column, DataRowVersion.Original] != column[recordNo]) { flag = true; } break; case LoadOption.Upsert: if (dataRow[column, DataRowVersion.Current] != column[recordNo]) { flag = true; } break; } } continue; Label_0058: if ((dataRow[column, DataRowVersion.Current] != column[recordNo]) || (dataRow[column, DataRowVersion.Original] != column[recordNo])) { flag = true; } } } DataRowChangeEventArgs args = null; DataRowAction nothing = DataRowAction.Nothing; int tempRecord = dataRow.tempRecord; dataRow.tempRecord = recordNo; switch (loadOption) { case LoadOption.OverwriteChanges: nothing = DataRowAction.ChangeCurrentAndOriginal; break; case LoadOption.PreserveChanges: if (dataRow.RowState != DataRowState.Unchanged) { nothing = DataRowAction.ChangeOriginal; break; } nothing = DataRowAction.ChangeCurrentAndOriginal; break; case LoadOption.Upsert: switch (dataRow.RowState) { case DataRowState.Unchanged: foreach (DataColumn column3 in dataRow.Table.Columns) { if (column3.Compare(dataRow.newRecord, recordNo) != 0) { nothing = DataRowAction.Change; break; } } goto Label_01A4; } nothing = DataRowAction.Change; break; default: throw ExceptionBuilder.ArgumentOutOfRange("LoadOption"); } Label_01A4:; try { args = this.RaiseRowChanging(null, dataRow, nothing); if (nothing == DataRowAction.Nothing) { dataRow.inChangingEvent = true; try { args = this.OnRowChanging(args, dataRow, nothing); } finally { dataRow.inChangingEvent = false; } } } finally { if (DataRowState.Detached == dataRow.RowState) { if (-1 != tempRecord) { this.FreeRecord(ref tempRecord); } } else if (dataRow.tempRecord != recordNo) { if (-1 != tempRecord) { this.FreeRecord(ref tempRecord); } if (-1 != recordNo) { this.FreeRecord(ref recordNo); } recordNo = dataRow.tempRecord; } else { dataRow.tempRecord = tempRecord; } } if (dataRow.tempRecord != -1) { dataRow.CancelEdit(); } switch (loadOption) { case LoadOption.OverwriteChanges: this.SetNewRecord(dataRow, recordNo, DataRowAction.Change, false, false, false); this.SetOldRecord(dataRow, recordNo); break; case LoadOption.PreserveChanges: if (dataRow.RowState != DataRowState.Unchanged) { this.SetOldRecord(dataRow, recordNo); break; } this.SetOldRecord(dataRow, recordNo); this.SetNewRecord(dataRow, recordNo, DataRowAction.Change, false, false, false); break; case LoadOption.Upsert: if (dataRow.RowState != DataRowState.Unchanged) { if (dataRow.RowState == DataRowState.Deleted) { dataRow.RejectChanges(); } this.SetNewRecord(dataRow, recordNo, DataRowAction.Change, false, false, false); break; } this.SetNewRecord(dataRow, recordNo, DataRowAction.Change, false, false, false); if (!dataRow.HasChanges()) { this.SetOldRecord(dataRow, recordNo); } break; default: throw ExceptionBuilder.ArgumentOutOfRange("LoadOption"); } if (flag) { string error = System.Data.Res.GetString("Load_ReadOnlyDataModified"); if (dataRow.RowError.Length == 0) { dataRow.RowError = error; } else { dataRow.RowError = dataRow.RowError + " ]:[ " + error; } foreach (DataColumn column2 in this.Columns) { if (column2.ReadOnly && !column2.Computed) { dataRow.SetColumnError(column2, error); } } } args = this.RaiseRowChanged(args, dataRow, nothing); if (nothing == DataRowAction.Nothing) { dataRow.inChangingEvent = true; try { this.OnRowChanged(args, dataRow, nothing); } finally { dataRow.inChangingEvent = false; } } }