Beispiel #1
0
        public override void Load(string fileName)
        {
            //Delete first the existent relations
            dao.Database  db        = App.Application.CurrentDb();
            dao.Relations relations = db.Relations;
            foreach (dao.Relation item in relations)
            {
                relations.Delete(item.Name);
            }
            relations.Refresh();

            using (StreamReader sr = new StreamReader(fileName)) {
                ImportObject import = new ImportObject(sr);
                import.ReadLine(2);      //Read 'Begin Relations' and 'Begin Relation' lines

                do
                {
                    string relationName = import.PeekObjectName();
                    Dictionary <string, object> relationProperties = import.ReadProperties();

                    dao.Relation relation = db.CreateRelation(relationName);
                    relation.Attributes   = Convert.ToInt32(relationProperties["Attributes"]);
                    relation.ForeignTable = Convert.ToString(relationProperties["ForeignTable"]);
                    relation.Table        = Convert.ToString(relationProperties["Table"]);
                    //try { relation.PartialReplica = Convert.ToBoolean(relationProperties["PartialReplica"]); } catch { }  //Accessing this property causes an exception ¿?

                    import.ReadLine(2);         //Read 'Begin Fields' and 'Begin Field' lines
                    while (!import.IsEnd)
                    {
                        dao.Field field = relation.CreateField();
                        field.Name = import.PropertyValue();
                        import.ReadLine();
                        field.ForeignName = import.PropertyValue();
                        import.ReadLine(2);     //Read 'End Field' and ('Begin Field' or 'End Fields'

                        relation.Fields.Append(field);
                    }

                    import.ReadLine(2);         //Read 'End Relation' and ('Begin Relation or 'End Relations')
                    relations.Append(relation);
                } while (!import.IsEnd);
            }
        }
Beispiel #2
0
        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);
                }
            }
        }