Пример #1
0
        /// <summary>
        /// Returns a DataTable that describes the column metadata of the DBase file.
        /// </summary>
        /// <returns>A DataTable that describes the column metadata.</returns>
        public DataTable GetSchemaTable()
        {
            DataTable schema = DbaseSchema.CreateSchemaTable();

            foreach (DbaseField dbf in _dbaseColumns)
            {
                schema.Columns.Add(dbf.ColumnName, dbf.DataType);
            }

            for (int i = 0; i < _dbaseColumns.Length; i++)
            {
                DataRow r = schema.NewRow();
                r[DbaseSchema.ColumnNameColumn]       = _dbaseColumns[i].ColumnName;
                r[DbaseSchema.ColumnSizeColumn]       = _dbaseColumns[i].Length;
                r[DbaseSchema.ColumnOrdinalColumn]    = i;
                r[DbaseSchema.NumericPrecisionColumn] = _dbaseColumns[i].Decimals;
                r[DbaseSchema.NumericScaleColumn]     = 0;
                r[DbaseSchema.DataTypeColumn]         = _dbaseColumns[i].DataType;
                r[DbaseSchema.AllowDBNullColumn]      = true;
                r[DbaseSchema.IsReadOnlyColumn]       = true;
                r[DbaseSchema.IsUniqueColumn]         = false;
                r[DbaseSchema.IsRowVersionColumn]     = false;
                r[DbaseSchema.IsKeyColumn]            = false;
                r[DbaseSchema.IsAutoIncrementColumn]  = false;
                r[DbaseSchema.IsLongColumn]           = false;

                // specializations, if ID is unique
                //if (_ColumnNames[i] == "ID")
                //	r["IsUnique"] = true;

                schema.Rows.Add(r);
            }

            return(schema);
        }
Пример #2
0
 public DbaseWriter(string filename)
 {
     _dbaseFileStream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
     _dbaseWriter     = new BinaryWriter(_dbaseFileStream, Encoding.ASCII);
     _dbaseReader     = new BinaryReader(_dbaseFileStream);
     if (_dbaseFileStream.Length > 0)
     {
         _header = DbaseHeader.ParseDbfHeader(_dbaseReader);
         _schema = DbaseSchema.GetFeatureTableForFields(_header.Columns);
     }
 }
Пример #3
0
 public DbaseWriter(FileStream file, DbaseField[] fields)
 {
     _dbaseFileStream = file;
     _dbaseWriter     = new BinaryWriter(_dbaseFileStream, Encoding.ASCII);
     if (file.CanRead)
     {
         _dbaseReader = new BinaryReader(_dbaseFileStream);
     }
     _header.Columns = fields;
     _schema         = DbaseSchema.GetFeatureTableForFields(fields);
     writeFullHeader();
 }
Пример #4
0
 public DbaseWriter(FileStream file, DataTable schema)
 {
     _dbaseFileStream = file;
     _dbaseWriter     = new BinaryWriter(_dbaseFileStream, Encoding.ASCII);
     if (file.CanRead)
     {
         _dbaseReader = new BinaryReader(_dbaseFileStream);
     }
     _schema         = schema;
     _header.Columns = DbaseSchema.GetFields(schema);
     writeFullHeader();
 }
Пример #5
0
        private void writeFullHeader()
        {
            _dbaseWriter.Seek(0, SeekOrigin.Begin);
            _dbaseWriter.Write(DbaseConstants.DbfVersionCode);
            _dbaseWriter.Write(new byte[3] {
                (byte)(_header.LastUpdate.Year - DbaseConstants.DbaseEpoch), (byte)_header.LastUpdate.Month, (byte)_header.LastUpdate.Day
            });
            _dbaseWriter.Write(_header.RecordCount);
            _dbaseWriter.Write(_header.HeaderLength);
            _dbaseWriter.Write(_header.RecordLength);
            _dbaseWriter.Write(new byte[DbaseConstants.EncodingOffset - (int)_dbaseWriter.BaseStream.Position]);
            _dbaseWriter.Write(_header.LanguageDriver);
            _dbaseWriter.Write(new byte[2]);
            foreach (DbaseField field in _header.Columns)
            {
                string colName      = field.ColumnName + new String('\0', DbaseConstants.FieldNameLength);
                byte[] colNameBytes = Encoding.ASCII.GetBytes(colName.Substring(0, DbaseConstants.FieldNameLength));
                _dbaseWriter.Write(colNameBytes);
                char fieldTypeCode = DbaseSchema.GetFieldTypeCode(field.DataType);
                _dbaseWriter.Write(fieldTypeCode);
                _dbaseWriter.Write(field.Address);

                if (fieldTypeCode == 'N' || fieldTypeCode == 'F')
                {
                    _dbaseWriter.Write((byte)field.Length);
                    _dbaseWriter.Write(field.Decimals);
                }
                else
                {
                    _dbaseWriter.Write((byte)field.Length);
                    _dbaseWriter.Write((byte)(field.Length >> 8));
                }

                _dbaseWriter.Write(new byte[14]);
            }
            _dbaseWriter.Write(DbaseConstants.HeaderTerminator);
        }