Beispiel #1
0
        private dao.Index ReadIndex(ImportObject import)
        {
            dao.Index idx = tableDef.CreateIndex();
            Dictionary <string, object> props = import.ReadProperties();

            idx.Name        = Convert.ToString(props["Name"]);
            idx.Primary     = Convert.ToBoolean(props["Primary"]);
            idx.Unique      = Convert.ToBoolean(props["Unique"]);
            idx.IgnoreNulls = Convert.ToBoolean(props["IgnoreNulls"]);
            idx.Required    = Convert.ToBoolean(props["Required"]);

            import.ReadLine(); //Read the 'Begin Fields' line
            while (!import.IsEnd)
            {
                dao.Field fld = idx.CreateField();
                import.ReadLine();
                fld.Name = import.PropertyValue();
                import.ReadLine();
                fld.Attributes = Convert.ToInt32(import.PropertyValue());
                ((dao.IndexFields)idx.Fields).Append(fld);
                import.ReadLine(2);  //Skip the 'End Field' line and read the next 'Begin Field' line (or 'End Fields' if there aren't more fields)
            }
            import.ReadLine(2);      //Read the 'End Index' line and the 'Begin Index' or 'End Indexes'
            return(idx);
        }
Beispiel #2
0
        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!!
        }
Beispiel #3
0
        public override void Load(string fileName)
        {
            Dictionary <string, object> databaseProperties = null;

            using (StreamReader sr = new StreamReader(fileName)) {
                ImportObject import = new ImportObject(sr);
                import.ReadLine(2);
                databaseProperties = import.ReadProperties();
            }

            if ((Options as OptionsDatabase).OverwriteDatabase)
            {
                App.CloseDatabase();
                int i = 0;
                do
                {
                    try {
                        System.Threading.Thread.Sleep(100);
                        File.Delete(App.FileName);
                    } catch {
                        i++;
                    }
                } while (i < 5);
                App.CreateDatabase(databaseProperties);
            }
            else
            {
                ClearProperties();
            }
            InternalLoad(databaseProperties);
        }
Beispiel #4
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 #5
0
        /// <summary>
        /// Ovewrite the current references with the references from <paramref name="fileName"/>
        /// </summary>
        /// <param name="fileName">Name of the file with the references to load into the database</param>
        public override void Load(string fileName)
        {
            using (StreamReader sr = new StreamReader(fileName)) {
                ImportObject import = new ImportObject(sr);

                //Remove any previous reference
                Access.Reference[] referencesList = new Access.Reference[App.Application.References.Count];
                int i = 0;
                foreach (Access.Reference reference in App.Application.References)
                {
                    if (!reference.BuiltIn && !reference.IsBroken)
                    {
                        referencesList[i++] = reference;
                    }
                }
                for (int j = 0; j < i; j++)
                {
                    App.Application.References.Remove(referencesList[j]);
                }

                //Add new references from file
                //if reference is dll or typelib, it will have guid and version
                //if reference is an access file (mde,mdb,mda,adp,...) we need add the reference by filename
                import.ReadLine();
                import.ReadLine();  //Read the Begin Reference (or End References if there is no references)
                while (!import.IsEnd)
                {
                    import.ReadLine();  //Read FullPath
                    string fullPath = import.PropertyValue();
                    import.ReadLine();  //Read Gui or End Reference
                    if (import.IsEnd)
                    {
                        fullPath = ResolveFileName(fullPath);
                        App.Application.References.AddFromFile(fullPath);
                    }
                    else
                    {
                        string guid = import.PropertyValue();
                        import.ReadLine();  //Read Major
                        int major = Convert.ToInt32(import.PropertyValue());
                        import.ReadLine();  //Read Minor
                        int minor = Convert.ToInt32(import.PropertyValue());
                        import.ReadLine();  //Read End Reference
                        App.Application.References.AddFromGuid(guid, major, minor);
                        import.ReadLine();  //Read the next Begin Reference or End References
                    }
                }
            }
        }
Beispiel #6
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);
                }
            }
        }