private void createLongControls() { dbFieldAtt attribute = Attribute.GetCustomAttribute( accessory.GetType().GetProperty(propertyName), typeof(dbFieldAtt)) as dbFieldAtt; if (attribute != null && attribute.dbObjectType != null) { DataTable sourceTable = new DataTable(); sourceTable.Columns.AddRange(new[] { new DataColumn("Number", typeof(int)), new DataColumn("Description", typeof(string)) }); MobileTable visualTable = MainProcess.CreateTable("Table", 200, 65); visualTable.OnChangeSelectedRow += visualTable_OnChangeSelectedRow; visualTable.DT = sourceTable; visualTable.AddColumn("№", "Number", 34); visualTable.AddColumn("Назва", "Description", 180); string command = string.Format("SELECT Id,Description FROM {0} WHERE MarkForDeleting=0", attribute.dbObjectType.Name); DataTable table = null; using (SqlCeCommand query = dbWorker.NewQuery(command)) { table = query.SelectToTable(); } foreach (DataRow row in table.Rows) { visualTable.AddRow(row["Id"], row["Description"]); } visualTable.Focus(); controls.Add(visualTable); } else { MainProcess.CreateLabel("Справочник/Документ пуст!", 5, 150, 230, MobileFontSize.Normal, MobileFontPosition.Center, MobileFontColors.Warning, FontStyle.Bold); controls.Add(MainProcess.CreateTable("Table", 200, 65)); } }
/// <summary>Получить не синхронизированные связанные данные</summary> /// <param name="type">Тип объекта</param> /// <param name="whereClause">where клауза</param> /// <returns>SQL команда для получения данных</returns> private string getUnsyncLinkedData(Type type, string whereClause) { StringBuilder selectClause = new StringBuilder(); StringBuilder fromClause = new StringBuilder(); PropertyInfo[] fields = type.GetProperties(); foreach (PropertyInfo field in fields) { dbFieldAtt attribute = Attribute.GetCustomAttribute(field, typeof(dbFieldAtt)) as dbFieldAtt; if (attribute != null) { if (attribute.dbObjectType == null) { selectClause.AppendFormat(" [{0}].[{1}],\r\n", type.Name, field.Name); } else { selectClause.AppendFormat(" [{0}].[{1}] [{2}],\r\n", attribute.dbObjectType.Name, dbObject.SYNCREF_NAME, field.Name); fromClause.AppendFormat("LEFT JOIN [{0}] ON [{0}].[{1}]=[{2}].[{3}]\r\n", attribute.dbObjectType.Name, dbObject.IDENTIFIER_NAME, type.Name, field.Name); } } } string command = string.Format("SELECT\r\n{0}\r\nFROM [{1}]\r\n{2}\r\nWHERE\r\n {3}", selectClause.Length > 3 ? selectClause.ToString(0, selectClause.Length - 3) : string.Empty, type.Name, fromClause.Length > 2 ? fromClause.ToString(0, fromClause.Length - 2) : string.Empty, whereClause); return(command); }
/// <summary>Создание объектов синхронизации</summary> /// <param name="table">Данные о объектах</param> /// <param name="skipExists">Пропустить существующие</param> /// <param name="deferredProperty">Список отложеных свойств</param> /// <param name="updId">Нужно обновить ID</param> private static void CreateSyncObject <T>(CatalogSynchronizer synchronizer, DataTable table, bool skipExists, ref List <DataAboutDeferredProperty> deferredProperty, bool updId) where T : dbObject { if (synchronizer != null) { int rowsCount = table.Rows.Count; for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++) { DataRow row = table.Rows[rowIndex]; synchronizer.Merge(row); //if (rowIndex % 10 == 0) { Trace.WriteLine(string.Format("{1} %, rowIndex = {0} from {2}", rowIndex, (int)(100 * rowIndex / rowsCount), rowsCount)); } //if (rowIndex % 500 == 0) // { // GC.Collect(); // GC.WaitForPendingFinalizers(); // } } return; } Type type = typeof(T); PropertyInfo[] properties = type.GetProperties(); string syncRef = string.Empty; string syncRefName = dbObject.SYNCREF_NAME.ToLower(); int lastDeferredIndex = deferredProperty.Count; foreach (DataRow row in table.Rows) { object newObj = Activator.CreateInstance(typeof(T)); T newObject = (T)newObj; bool needDeferred = false; foreach (PropertyInfo property in properties) { dbFieldAtt attribute = Attribute.GetCustomAttribute(property, typeof(dbFieldAtt)) as dbFieldAtt; if (attribute != null && table.Columns.Contains(property.Name)) { object value = row[property.Name]; //Не существует ли елемент с такой ссылкой? if (property.Name.ToLower().Equals(syncRefName)) { if (BarcodeWorker.IsRefExist(type, value.ToString())) { if (skipExists) { break; } newObject.SetNotNew(); } syncRef = value.ToString(); } else if (updId && property.Name.ToLower().Equals(dbObject.IDENTIFIER_NAME)) { continue; } if (property.PropertyType == typeof(int)) { value = string.IsNullOrEmpty(value.ToString()) ? 0 : Convert.ToInt32(value); } else if (property.PropertyType == typeof(double)) { value = string.IsNullOrEmpty(value.ToString()) ? 0D : Convert.ToDouble(value); } else if (property.PropertyType == typeof(long)) { if (attribute.dbObjectType == null) { value = string.IsNullOrEmpty(value.ToString()) ? 0L : Convert.ToInt64(value); } else { if (value != null && !string.IsNullOrEmpty(value.ToString())) { DataAboutDeferredProperty data = new DataAboutDeferredProperty(type, attribute.dbObjectType, property.Name, value); deferredProperty.Add(data); needDeferred = true; } value = 0L; } } else if (property.PropertyType == typeof(bool)) { value = Convert.ToBoolean(value); } else if (property.PropertyType == typeof(DateTime)) { value = StringParser.ParseDateTime(value); } else if (property.PropertyType.IsEnum) { value = Enum.Parse(property.PropertyType, value.ToString(), false); } else if (property.PropertyType == typeof(string)) { string fullValue = value.ToString(); value = value.ToString(); if (property.Name != CatalogObject.DESCRIPTION) { int length = attribute.StrLength == 0 ? dbFieldAtt.DEFAULT_STR_LENGTH : attribute.StrLength; if (fullValue.Length > length) { value = fullValue.Substring(0, length); } } } property.SetValue(newObject, value, null); } } ISynced syncObject = newObject as ISynced; if (syncObject != null) { syncObject.IsSynced = true; } if (updId && !skipExists && !string.IsNullOrEmpty(syncRef)) { newObject.Id = Convert.ToInt64(BarcodeWorker.GetIdByRef(type, syncRef)); } CatalogObject catalog = newObject as CatalogObject; if (catalog != null) { dbElementAtt attribute = Attribute.GetCustomAttribute(newObject.GetType(), typeof(dbElementAtt)) as dbElementAtt; int length = attribute == null || attribute.DescriptionLength == 0 ? dbElementAtt.DEFAULT_DES_LENGTH : attribute.DescriptionLength; if (catalog.Description.Length > length) { catalog.Description = catalog.Description.Substring(0, length); } } newObject.Sync <T>(updId); if (needDeferred) { for (int i = lastDeferredIndex; i < deferredProperty.Count; i++) { deferredProperty[i].Id = newObject.Id; } lastDeferredIndex = deferredProperty.Count; } } }