protected override Dictionary <string, IPropertyTransform> gatherProperties() { //Add known properties with special treatment //other properties are not allways present. If they are present add them and write with NoActionTransform //Use NullTransform for read only properties that do not should be written Dictionary <string, IPropertyTransform> propsToWrite = new Dictionary <string, IPropertyTransform>(); NullTransform nullTransform = new NullTransform(); NoActionDataTypeTransoform noActionTransform = new NoActionDataTypeTransoform(); propsToWrite.Add("Name", new FileNameDataTypeTransform()); propsToWrite.Add("Transactions", nullTransform); propsToWrite.Add("RecordsAffected", nullTransform); propsToWrite.Add("Build", nullTransform); propsToWrite.Add("Updatable", nullTransform); propsToWrite.Add("Connection", nullTransform); propsToWrite.Add("Version", nullTransform); dao.Database db = App.Application.CurrentDb(); foreach (dao.Property prop in db.Properties) { if (!propsToWrite.ContainsKey(prop.Name)) { propsToWrite.Add(prop.Name, noActionTransform); } } return(propsToWrite); }
public override void Save(string fileName) { //Make sure the path exists MakePath(System.IO.Path.GetDirectoryName(fileName)); using (StreamWriter sw = new StreamWriter(fileName)) { ExportObject export = new ExportObject(sw); dao.Database db = App.Application.CurrentDb(); export.WriteBegin(ClassName); foreach (dao.Relation relation in db.Relations) { export.WriteBegin("Relation", relation.Name); export.WriteProperty("Attributes", relation.Attributes); export.WriteProperty("ForeignTable", relation.ForeignTable); export.WriteProperty("Table", relation.Table); //try { export.WriteProperty("PartialReplica", relation.PartialReplica); } catch { } //Accessing this property causes an exception ¿? export.WriteBegin("Fields"); foreach (dao.Field fld in relation.Fields) { export.WriteBegin("Field"); export.WriteProperty("Name", fld.Name); export.WriteProperty("ForeignName", fld.ForeignName); export.WriteEnd(); } export.WriteEnd(); export.WriteEnd(); } export.WriteEnd(); } }
private void OnFormClosed(object sender, FormClosedEventArgs e) { global.SaveFormSettings(this); _dbData.Close(); _dbData = null; _dbe = null; }
private void OnFormLoad(object sender, EventArgs e) { global.LoadFormSettings(this, true); group2.Visible = false; Size = new Size(group1.Width + (group1.Left * 5), Height); _dbe = new DBEngine(); _dbData = _dbe.OpenDatabase(global.MDBPath); }
private bool ExistsTableDef(dao.Database db, string tableName) { foreach (dao.TableDef tbDef in db.TableDefs) { if (string.Compare(tbDef.Name, tableName, true) == 0) { return(true); } } return(false); }
private List <dao.Relation> GetTableRelations(dao.Database db) { List <dao.Relation> result = new List <Relation>(); foreach (dao.Relation relation in db.Relations) { if (relation.Table == this.Name || relation.ForeignTable == this.Name) { result.Add(relation); } } return(result); }
/// <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 bool ResetReferenceNumbers() { var dbe = new DBEngine(); dao.Database db = dbe.OpenDatabase(global.MDBPath); try { try { db.QueryDefs.Delete("qry_tempResetRefNo1"); } catch { } try { db.QueryDefs.Delete("qry_tempResetRefNo2"); } catch { } string sql = @"SELECT Left([RefNo],InStr(InStr(1,[RefNo],'-')+1,[RefNo],'-')-1) AS code, CLng(Right([RefNo],Len(Left([RefNo],InStr(InStr(1,[RefNo],'-')+1,[RefNo],'-')-1))-3)) AS [counter] FROM tblSampling"; db.CreateQueryDef("qry_tempResetRefNo1", sql); sql = @"SELECT qry_tempResetRefNo1.code, Max(qry_tempResetRefNo1.counter) AS [max] From qry_tempResetRefNo1 GROUP BY qry_tempResetRefNo1.code"; db.CreateQueryDef("qry_tempResetRefNo2", sql); sql = "Delete * from tblRefCodeCounter"; db.Execute(sql); sql = @"INSERT INTO tblRefCodeCounter ( GearRefCode, [Counter] ) SELECT qry_tempResetRefNo2.code, [Max]+1 AS countermax FROM qry_tempResetRefNo2"; db.Execute(sql); db.QueryDefs.Delete("qry_tempResetRefNo1"); db.QueryDefs.Delete("qry_tempResetRefNo2"); return(true); } catch (Exception ex) { Logger.LogError(ex); return(false); } }
/// <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>(); }
/// <summary> /// Helper function to create the inland grid table /// </summary> /// <param name="dbData"></param> /// <returns></returns> private static bool CreateInlandGridTable(dao.Database dbData) { var sql = @"Create table tblGrid25Inland (grid_name TEXT(8), zone TEXT(3), x DOUBLE, y DOUBLE)"; dbData.Execute(sql); sql = "Create index idxPrimary on tblGrid25Inland (grid_name, zone) with PRIMARY"; dbData.Execute(sql); return(true); }
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; } }
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); } }
/// <summary> /// Save the database properties to a file /// </summary> /// <param name="fileName">Name of the file where to save the properties</param> /// <remarks> /// <para> /// To facilitate subsequent read operations include the data type of the property at the beginning, as part of property value. /// The property is written in the format "propertyName: DataType,propertyValue" with no spaces between dataType and propertyValue. /// </para> /// <para> /// The xxxxxDataTypeTransform family has the same functionality than xxxxTransfor, /// except that inserts the data type before the propertyValue /// </para> /// </remarks> public override void Save(string fileName) { Dictionary <string, IPropertyTransform> propsToWrite = gatherProperties(); //Make sure the path exists MakePath(System.IO.Path.GetDirectoryName(fileName)); //Write the properties with help of an ExportObject dao.Database db = App.Application.CurrentDb(); using (StreamWriter sw = new StreamWriter(fileName)) { ExportObject export = new ExportObject(sw); string dbFileName = System.IO.Path.GetFileName(App.FileName); export.WriteBegin(ClassName, dbFileName); foreach (string item in propsToWrite.Keys) { propsToWrite[item].WriteTransform(export, db.Properties[item]); } export.WriteEnd(); } }
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); } } }