private string GetFieldTypeAsString(DAO.Field fd) { // Convert DAO.Type to a human-readable string. This can be converted like this: // ((DAO.DataTypeEnum)DAO.Field .Type).ToString()) // but returns something like "dbText", when we want to display "Text" (or similar). // Alternatively, use a giant switch: /* switch (type) * { * case (short)DAO.DataTypeEnum.dbBoolean: * strType = "Boolean"; * break; * // etc... */ string strType = "Unknown"; try { strType = ((DAO.DataTypeEnum)fd.Type).ToString().Replace("db", ""); } catch (Exception ex) { Console.WriteLine(UtilitiesGeneral.FormatException( this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message)); } return(strType); }
static void Main(string[] args) { DAO.DBEngine dbEng = new DAO.DBEngine(); DAO.Workspace ws = dbEng.CreateWorkspace("", "admin", "", DAO.WorkspaceTypeEnum.dbUseJet); DAO.Database db = ws.OpenDatabase("z:\\docs\\dbfrom.mdb", false, false, ""); DAO.TableDef tdf = db.TableDefs["Test"]; DAO.Field fld = tdf.Fields["AYesNo"]; //dbInteger 3 //accheckbox 106 DAO.Property prp = fld.CreateProperty("DisplayControl", 3, 106); fld.Properties.Append(prp); }
public bool DoesFieldExist(DAO.Database db, string strTable, string strField) { // DAO: HelperBoolFieldToString function to determine if a field exists try { DAO.Field ThisField = db.TableDefs[strTable].Fields[strField]; return(true); } catch (Exception ex) { Console.WriteLine(UtilitiesGeneral.FormatException( this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message)); } return(false); }
private bool IsPrimaryKey(DAO.Field Fld, DAO.TableDef Tbl) { // checks if fld is primary key foreach (DAO.Index IDX in Tbl.Indexes) { if (IDX.Primary) { foreach (DAO.Field IDXfld in IDX.Fields) { if (IDXfld.Name == Fld.Name) { return(true); } } } } return(false); }
public bool IsAutoIncrField(DAO.Field Fld) { return(((int)(DAO.FieldAttributeEnum.dbAutoIncrField) & Fld.Attributes) == (int)(DAO.FieldAttributeEnum.dbAutoIncrField)); }
private static DateTime CreateTableTime = DateTime.MinValue; //最后一次创建表的时间 #region IDb 成员 public void CreateTable(Table table, string tableName) { CreateTableTime = DateTime.Now; if (tableName == null || tableName.Length == 0) { tableName = table.Code; } DAO.DBEngine daoDBE = new DAO.DBEngine(); DAO.Database daoDB = null; daoDB = daoDBE.OpenDatabase(this.FileName, false, false, "MS ACCESS;PWD=" + this.Password); DAO.TableDefs daoTables = daoDB.TableDefs; foreach (DAO.TableDef daoT in daoDB.TableDefs) { if (daoT.Name.Equals(tableName)) { daoTables.Delete(tableName); //删除现存的表 } } DAO.TableDef daoTable = daoDB.CreateTableDef(tableName, 0, "", ""); string strPrimaryKeyFields = ""; foreach (Column _Column in table.Columns) { DAO.Field daoField = new DAO.Field(); daoField.Name = _Column.Code; switch (_Column.DataType) { case DataTypeOptions.Int: daoField.Type = Convert.ToInt16(DAO.DataTypeEnum.dbLong); break; case DataTypeOptions.Long: case DataTypeOptions.Decimal: daoField.Type = Convert.ToInt16(DAO.DataTypeEnum.dbDouble); break; case DataTypeOptions.VarChar: if (_Column.DataLength > 255) { daoField.Type = Convert.ToInt16(DAO.DataTypeEnum.dbMemo); daoField.AllowZeroLength = true; } else { daoField.Type = Convert.ToInt16(DAO.DataTypeEnum.dbText); daoField.Size = _Column.DataLength; daoField.AllowZeroLength = true; } break; case DataTypeOptions.Text: daoField.Type = Convert.ToInt16(DAO.DataTypeEnum.dbMemo); daoField.AllowZeroLength = true; break; case DataTypeOptions.File: daoField.Type = Convert.ToInt16(DAO.DataTypeEnum.dbLongBinary); break; default: throw new Exception("尚未实现的数据类型 " + _Column.DataType); } daoField.Required = _Column.IsNotNull; if (_Column.IsPrimaryKey) { strPrimaryKeyFields += _Column.Code + ";"; } daoTable.Fields.Append(daoField); } daoDB.TableDefs.Append(daoTable); if (table.Name != null && table.Name.Length > 0) { DAO.Property daoTableProperty = daoTable.CreateProperty("Description", DAO.DataTypeEnum.dbText, table.Name, 0); daoTable.Properties.Append(daoTableProperty); } foreach (Column _Column in table.Columns) { if (_Column.Name != null && _Column.Name.Length > 0) { DAO.Field daoField = daoTable.Fields[_Column.Code]; DAO.Property daoColumnProperty = daoField.CreateProperty("Description", DAO.DataTypeEnum.dbText, _Column.Name, 0); daoField.Properties.Append(daoColumnProperty); } } if (strPrimaryKeyFields.Length > 0) { DAO.Index daoIndex = daoTable.CreateIndex("PK_" + tableName); daoIndex.Fields = strPrimaryKeyFields; daoIndex.Primary = true; daoIndex.Unique = true; daoTable.Indexes.Append(daoIndex); } foreach (Index _Index in table.Indexs) { string strKeyFields = ""; foreach (Column _KeyColumn in _Index.Columns) { strKeyFields += "+" + _KeyColumn.Code + ";"; } if (strKeyFields.Length > 0) { DAO.Index daoIndex = daoTable.CreateIndex(_Index.Code); daoIndex.Fields = strKeyFields; daoIndex.Primary = false; daoIndex.Unique = false; daoTable.Indexes.Append(daoIndex); } } daoDB.Close(); TimeSpan ts = DateTime.Now - CreateTableTime; if (ts.Seconds < 2) { System.Threading.Thread.Sleep(10000); //Access 数据表创建后需要一段时间才能访问。 } }
public void Insert(Type t, System.Collections.IEnumerable data, bool insertDefaultColumns) { // temporarily close the connection to the file bool wasOpen = false; if (this.Connection.State != ConnectionState.Closed) { wasOpen = true; this.Connection.Close(); } // get the properties of the type we're inserting var attr = MappedObjectAttribute.GetAttribute <MappedClassAttribute>(t); if (attr == null) { throw new Exception(string.Format("Type {0}.{1} could not be automatically inserted.", t.Namespace, t.Name)); } attr.InferProperties(t); var props = attr.Properties.Where(p => p.Include).ToArray(); // setup DAO var engine = new DAO.DBEngine(); var db = engine.OpenDatabase(this.Connection.DataSource); var recordSet = db.OpenRecordset(attr.Name); var fields = new DAO.Field[props.Length]; var fieldNames = recordSet.Fields .OfType <DAO.Field>() .Select(f => f.Name); for (int i = 0; i < fields.Length; ++i) { fields[i] = recordSet.Fields[props[i].Name]; } // copy foreach (var obj in data) { recordSet.AddNew(); for (int j = 0; j < fields.Length; ++j) { if (!props[j].IsIdentity || insertDefaultColumns) { var value = props[j].GetValue(obj); if (props[j].SystemType == typeof(Guid)) { value = string.Format("{{{0}}}", value); } fields[j].Value = value; } } recordSet.Update(); } // clean up recordSet.Clone(); db.Close(); // reopen the connection, if the user is expecting it. if (wasOpen) { this.Connection.Open(); } }
public Field(DAO.Field _fld) { fld=_fld; }