예제 #1
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 数据表创建后需要一段时间才能访问。
            }
        }
예제 #2
0
		public Index(DAO.Index _ind)
		{
			ind=_ind; 
		}