public void LoadProperties(dao.TableDef tableDef, ImportObject import) { props = import.ReadProperties(); import.ReadLine(); //Reads the 'End Field' line daoField.Attributes = Convert.ToInt32(props["Attributes"]); //CollatingOrder is read only!! daoField.Type = Convert.ToInt16(props["Type"]); daoField.Name = Convert.ToString(props["Name"]); daoField.OrdinalPosition = Convert.ToInt16(props["OrdinalPosition"]); daoField.Size = Convert.ToInt32(props["Size"]); //SourceField, SourceTable, DataUpdatable are read only!! daoField.DefaultValue = Convert.ToString(props["DefaultValue"]); daoField.ValidationRule = Convert.ToString(props["ValidationRule"]); daoField.ValidationText = Convert.ToString(props["ValidationText"]); daoField.Required = Convert.ToBoolean(props["Required"]); //AllowZeroLength property is valid only for text fields if (daoField.Type == (short)dao.DataTypeEnum.dbText) { daoField.AllowZeroLength = Convert.ToBoolean(props["AllowZeroLength"]); } //VisibleValue is read only!! }
/// <summary> /// Save table defintion to <paramref name="fileName"/> /// </summary> /// <param name="fileName">File name where save the table definition</param> public override void Save(string fileName) { MakePath(System.IO.Path.GetDirectoryName(fileName)); dao.DBEngine dbEngine = new dao.DBEngine(); dao.Database db = dbEngine.OpenDatabase(App.FileName); dao.TableDef tbDef = db.TableDefs[Name]; using (StreamWriter sw = new StreamWriter(fileName)) { ExportObject export = new ExportObject(sw); //export.ListProperties(tbDef.Name, tbDef.Properties); export.WriteBegin(ClassName, TableName); export.WriteProperty("Attributes", tbDef.Attributes); export.WriteProperty("Connect", tbDef.Connect); export.WriteProperty("SourceTableName", tbDef.SourceTableName); export.WriteProperty("ValidationRule", tbDef.ValidationRule); export.WriteProperty("ValidationText", tbDef.ValidationText); PropertyCollectionDao propColl = new PropertyCollectionDao(tbDef, tbDef.Properties); propColl.TryWriteProperty(export, "Description"); propColl.TryWriteProperty(export, "ConflictTable"); propColl.TryWriteProperty(export, "ReplicaFilter"); propColl.TryWriteProperty(export, "Orientation"); propColl.TryWriteProperty(export, "OrderByOn"); propColl.TryWriteProperty(export, "SubdatasheetName"); propColl.TryWriteProperty(export, "LinkChildFields"); propColl.TryWriteProperty(export, "LinkMasterFields"); propColl.TryWriteProperty(export, "SubdatasheetHeight"); propColl.TryWriteProperty(export, "SubdatasheetExpanded"); propColl.TryWriteProperty(export, "DefaultView"); propColl.TryWriteProperty(export, "OrderBy"); export.WriteBegin("Fields"); foreach (dao.Field field in tbDef.Fields) { export.WriteObject(new Field(field)); } export.WriteEnd(); //End Fields export.WriteBegin("Indexes"); //TODO: Add new option menu to ignore linked tables errors if the linked document do not exist // Check if the linked document exists before iterate the indexes collection foreach (dao.Index daoIndex in tbDef.Indexes) { export.WriteObject(new Index(daoIndex)); } export.WriteEnd(); //End Indexes export.WriteEnd(); //End Table } db.Close(); }
/// <summary> /// Constructor /// </summary> /// <param name="app">AccessScrCtrl application</param> /// <param name="name">Name of the object name</param> /// <param name="objectType">Access object type</param> public Table(AccessApp app, string name, ObjectType objectType) : base(app, name, objectType) { //TODO: Ver posibilidad de no instanciar tableDef hasta el método Load (habría que cambiar el método Save) dao.Database db = app.Application.CurrentDb(); if (ExistsTableDef(db, name.ToString())) { tableDef = app.Application.CurrentDb().TableDefs[name]; } else { tableDef = null; } TableName = name; Fields = new List <Field>(); }
public override void Load(string fileName) { if (!AllowDataLost && HasData) { throw new DataLostException(this.Name); } using (StreamReader sr = new StreamReader(fileName)) { ImportObject import = new ImportObject(sr); string objName = import.ReadObjectName(); if (string.Compare(this.Name, objName, true) != 0) { this.Name = objName; } dao.Database db = App.Application.CurrentDb(); bool tableExists = false; string tempTableName = this.Name; //if (ExistsTableDef(db, this.Name)) { // foreach (dao.Relation relation in GetTableRelations(db)) { // db.Relations.Delete(relation.Name); // } // db.TableDefs.Delete(this.Name); //} if (ExistsTableDef(db, this.Name)) { tableExists = true; tempTableName = String.Format("{0}{1}", this.Name, DateTime.Now.ToString("yyyyMMddHHmmssff")); tempTableName = tempTableName.Substring(tempTableName.Length - Math.Min(64, tempTableName.Length)); } tableDef = db.CreateTableDef(tempTableName); try { //read table properties Dictionary <string, object> props = import.ReadProperties(); //tableDef.Attributes = Convert.ToInt32(props["Attributes"]); tableDef.Connect = Convert.ToString(props["Connect"]); tableDef.SourceTableName = Convert.ToString(props["SourceTableName"]); tableDef.ValidationRule = Convert.ToString(props["ValidationRule"]); tableDef.ValidationText = Convert.ToString(props["ValidationText"]); //Linked tables do not allow fields nor indexes definitions //but ms access properties are allowed bool isLinkedTable = !String.IsNullOrEmpty(Convert.ToString(props["Connect"])); //read fields import.ReadLine(); //Read the 'Begin Fields' line while (!import.IsEnd) { dao.Field fld = ReadField(import); if (!isLinkedTable) { tableDef.Fields.Append(fld); } } if (!isLinkedTable) { //read indexes import.ReadLine(); //Read the 'Begin Indexes' line. If there is not indexes, CurrentLine == End if (import.IsBegin) { import.ReadLine(); //Read the 'Begin Index' line. while (!import.IsEnd) { dao.Index idx = ReadIndex(import); if (idx == null) { break; } tableDef.Indexes.Append(idx); } } } db.TableDefs.Append(tableDef); db.TableDefs.Refresh(); //According with MS-doc: The object to which you are adding the user-defined property must already be appended to a collection. //see: http://msdn.microsoft.com/en-us/library/ff820932.aspx //So: After fields added to the tableDef and tableDef added to the database, we add the custom properties //This properties are also available for linked tables foreach (Field field in Fields) { field.AddCustomProperties(); } AddCustomProperties(props); //manage table relations if (tableExists) { List <Relation> relationsList = new List <Relation>(); foreach (dao.Relation relation in GetTableRelations(db)) { dao.Relation newRelation = db.CreateRelation(relation.Name, relation.Table, relation.ForeignTable, relation.Attributes); //try { newRelation.PartialReplica = relation.PartialReplica; } catch { } //Accessing this property causes an exception ¿? foreach (dao.Field field in relation.Fields) { dao.Field newField = newRelation.CreateField(); newField.Name = field.Name; newField.ForeignName = field.ForeignName; newRelation.Fields.Append(newField); } relationsList.Add(newRelation); db.Relations.Delete(relation.Name); } db.Relations.Refresh(); db.TableDefs.Delete(this.Name); db.TableDefs[tempTableName].Name = this.Name; db.TableDefs.Refresh(); foreach (dao.Relation relation in relationsList) { try { db.Relations.Append(relation); } catch { //not allways we can restore the relation: the field do not exists or has changed the data type } } } } catch (Exception ex) { if (tableExists) { db.TableDefs.Delete(tempTableName); } string message = String.Format(AccessIO.Properties.ImportRes.ErrorAtLineNum, import.LineNumber, ex.Message); throw new WrongFileFormatException(message, fileName, import.LineNumber); } } }