示例#1
0
        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 static void Main(string[] args)
 {
     try
     {
         if (args.Length == 0)
         {
             Console.WriteLine("Please enter an MS Access application path as a parameter!");
             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. It's Value is '" + allowBypassKeyProperty.Value + "'");
         }
         else
         {
             allowBypassKeyProperty.Value = !allowBypassKeyProperty.Value;
             Console.WriteLine("AllowBypassKey set to '" + allowBypassKeyProperty.Value + "'!");
         }
     }
     catch (Exception exception)
     {
         Console.WriteLine("Exception Message: " + exception.Message);
         Console.WriteLine("Inner Exception:" + exception.InnerException);
     }
     finally
     {
         database.Close();
         System.Runtime.InteropServices.Marshal.ReleaseComObject(database);
         database = null;
         System.Runtime.InteropServices.Marshal.ReleaseComObject(dbEngine);
         dbEngine = null;
         Console.WriteLine();
         Console.WriteLine("Press enter to continue ...");
         Console.ReadLine();
     }
 }
		public Property(DAO.Property _prop)
		{
			prop=_prop;
		}
示例#4
0
文件: AccessDb.cs 项目: jujianfei/AC
        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 数据表创建后需要一段时间才能访问。
            }
        }