示例#1
0
        /// <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();
        }
 public static void Main(string[] args)
 {
     try
     {
         if (args.Length == 0)
         {
             Console.WriteLine("Please enter an MSAccess application path as a parameter!");
             Console.WriteLine();
             Console.WriteLine("Press enter to continue ...");
             Console.ReadLine();
             return;
         }
         dbEngine = new DAO.DBEngine();
         database = dbEngine.OpenDatabase(args[0]);
         DAO.Property allowBypassKeyProperty = null;
         foreach (dao.Property property in database.Properties)
         {
             if (property.Name == "AllowBypassKey")
             {
                 allowBypassKeyProperty = property;
                 break;
             }
         }
         if (allowBypassKeyProperty == null)
         {
             allowBypassKeyProperty = database.CreateProperty("AllowBypassKey", DAO.DataTypeEnum.dbBoolean, false, true);
             database.Properties.Append(allowBypassKeyProperty);
             Console.WriteLine("AllowBypassKey Property has been added.");
         }
         else
         {
             allowBypassKeyProperty.Value = !allowBypassKeyProperty.Value;
             Console.WriteLine("AllowBypassKey is now " + allowBypassKeyProperty.Value + "!");
         }
     }
     finally
     {
         database.Close();
         System.Runtime.InteropServices.Marshal.ReleaseComObject(database);
         database = null;
         System.Runtime.InteropServices.Marshal.ReleaseComObject(dbEngine);
         dbEngine = null;
     }
 }
示例#3
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);
            }
        }