예제 #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);
        }
예제 #2
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);
            }
        }
예제 #3
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);
                }
            }
        }
예제 #4
0
        public static void DefinirDescrTableOuColonne(string sCheminMdb, string sTable,
                                                      string sColonne, string sDescr)
        {
            // Modify the description property of an access table from .NET
            // https://itproblemy.pl/questions/37023427/modify-the-description-property-of-an-access-table-from-net

            const string sPropDescription     = "Description";
            int          iErrPropertyNotFound = -2146825018;

            try
            {
                // Ne fonctionne pas, car spécifique à MSAccess 2013
                // https://www.nuget.org/packages/Microsoft.Office.Interop.Access.Dao/
                // Du coup on doit conserver la dll dao.dll
                //var ws = new Microsoft.Office.Interop.Access.Dao.DBEngine();
                //Microsoft.Office.Interop.Access.Dao.Database db;
                //Microsoft.Office.Interop.Access.Dao.TableDef tbl;
                //Microsoft.Office.Interop.Access.Dao.Property prop;
                //Microsoft.Office.Interop.Access.Dao.Field fld = null;

                var          ws = new dao.DBEngine();
                dao.Database db;
                dao.TableDef tbl;
                dao.Property prop;
                dao.Field    fld = null;

                db  = ws.OpenDatabase(sCheminMdb);
                tbl = db.TableDefs[sTable];
                bool bChamp = false;
                if (sColonne.Length > 0)
                {
                    fld    = tbl.Fields[sColonne];
                    bChamp = true;
                }

                try // Pas de Properties.Contains dans dao ?
                {
                    if (bChamp)
                    {
                        prop = fld.Properties[sPropDescription];
                    }
                    else
                    {
                        prop = tbl.Properties[sPropDescription];
                    }
                    prop.Value = sDescr;
                }
                catch (System.Runtime.InteropServices.COMException ex)
                {
                    //const int idbText = 10; // dao.DataTypeEnum.dbText;
                    if (ex.ErrorCode == iErrPropertyNotFound)
                    {
                        if (bChamp)
                        {
                            fld.Properties.Append(
                                fld.CreateProperty(sPropDescription, DataTypeEnum.dbText, sDescr));
                        }
                        else
                        {
                            tbl.Properties.Append(
                                tbl.CreateProperty(sPropDescription, DataTypeEnum.dbText, sDescr));
                        }
                    }
                    else
                    {
                        throw;
                    }
                }

                // En Lecture
                //prop = tbl.Properties["Description"];
                //string sDescr = prop.Value;
                //Debug.WriteLine(sDescr);

                ws = null;
            }
            catch (Exception ex)
            {
                string sMsg = ex.Message;
                //string sMsgErrDet = Util.sLireExceptionInterne(ex);
                //sMsg += sMsgErrDet;
                sMsg += clsConstMdb.sCrLf + "Table : " + sTable;
                if (sColonne.Length > 0)
                {
                    sMsg += clsConstMdb.sCrLf + "Colonne : " + sColonne;
                }
                MessageBox.Show(sMsg, clsConstMdb.sNomAppli,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }