/// <summary> /// Returns foreign key ordinal in the type. /// </summary> /// <param name="Type"></param> /// <param name="Parent"></param> /// <returns></returns> private static int GetOrdinal(SqlCeUpdatableRecord DetailRecord, Type Type, Type Parent) { for (int i = 0; i < DetailRecord.FieldCount; i++) { string fieldName = DetailRecord.GetName(i).ToUpper(); if (fieldName == (Parent.Name + "_Id").ToUpper() || fieldName == (Parent.Name + "Id").ToUpper()) { return(i); } } throw new InvalidOperationException(string.Format("Ordinal for detail {0} was not found in {1}", Type.Name, Parent.Name)); }
private List <object> WalkObject(Object Input, ShipmentContext context, SqlCeResultSet resultSet) { bool firstOccurrence; gen.GetId(Input, out firstOccurrence); if (!firstOccurrence) { return(null); } CachedTableInfo metadata = new CachedTableInfo(Input.GetType().Name, context, resultSet); List <string> processedFields = new List <string>(); List <object> keys = new List <object>(); SqlCeUpdatableRecord r = resultSet.CreateRecord(); // Match the database with memory object. for (int i = 0; i < resultSet.FieldCount; i++) { if (!metadata.IsFieldKey(r.GetName(i))) { string fieldName = resultSet.GetName(i); PropertyInfo fi = Input.GetType().GetProperty(fieldName); if (fi != null) { r[i] = fi.GetValue(Input, null); processedFields.Add(fieldName); } else { throw new InvalidOperationException(string.Format("Field {0} was not found in the class {1}", fi.Name, Input.GetType().Name)); } } } resultSet.Insert(r, DbInsertOptions.PositionOnInsertedRow); foreach (var item in metadata.Keys) { keys.Add(r[item.Name]); } // keys now contain assigned autoincrement fields. return(keys); }
public int WriteToServerNew(DataTable table, DataRowState rowState, string str) { this.rowState = rowState; //CheckDestination(); int totalRows = 0; int errorCount = 0; if (this.mappings.Count < 1) { if (this.conn.State != ConnectionState.Open) { this.conn.Open(); } using (SqlCeCommand cmd = new SqlCeCommand(this.destination, this.conn)) { cmd.CommandType = CommandType.TableDirect; using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable)) { int idOrdinal = this.IdentityOrdinal(); // int offset = 0; SqlCeUpdatableRecord rec = rs.CreateRecord(); // DataTable dt = rs.GetSchemaTable(); //this.mappings.ValidateCollection(rec, table.Columns); int fieldCount = rec.FieldCount; //if (idOrdinal > -1) //{ // fieldCount = fieldCount - 1; // offset = 1; //} //if (table.Columns.Count - 3 != rec.FieldCount) //{ // throw new Exception("Field counts do not match"); //} int rowCounter = 0; //IdInsertOn(); //string[] colNames = new string[rec.FieldCount]; //for (int y = 0; y < rec.FieldCount; y++) //{ // colNames[y] = rec.GetName(y); //} int[] colIndex = new int[rec.FieldCount]; string colName; for (int y = 1; y < rec.FieldCount; y++) { colName = rec.GetName(y); if (colName.ToLower() == "rowstatus") { colIndex[y] = -1; } else { colIndex[y] = table.Columns[rec.GetName(y)].Ordinal; } } object value; foreach (DataRow row in table.Rows) { try { if ((row["Is_Deleted"] == DBNull.Value) || ((Convert.ToInt32(row["Is_Deleted"]) == 0) && (Convert.ToInt32(row["Is_Active"]) == 1))) { // Never process deleted rows //if (row.RowState == DataRowState.Deleted) // continue; //// if a specific rowstate is requested //if (this.rowState != 0) //{ // if (row.RowState != this.rowState) // continue; //} for (int y = 1; y < rec.FieldCount; y++) { // Let the destination assign identity values try { if (colIndex[y] == -1) { value = Convert.ToInt16(RowStatus.Synchronized); } else { value = row[colIndex[y]]; } if (value != null && value != DBNull.Value) { rec.SetValue(y, value); } else { if (keepNulls) { // rec.SetValue(i, DBNull.Value); rec.SetValue(y, DBNull.Value); } else { rec.SetDefault(y); } } // Fire event if needed if (this.notifyAfter > 0 && rowCounter == this.notifyAfter) { FireRowsCopiedEvent(totalRows); rowCounter = 0; } } catch (Exception iEx) { } } rowCounter++; rs.Insert(rec); totalRows++; } } catch (SqlCeException sqlex) { errorCount++; if (errorCount > 50) { throw sqlex; // break; } } catch (Exception ex) { errorCount++; if (errorCount > 50) { throw ex; //break; } } } // IdInsertOff(); } } } return(totalRows); }