Exemplo n.º 1
0
 protected internal DbfHeader(DbfTable owner)
 {
     this._owner                = owner;
     this._owner.RecordAdded   += new EventHandler(this.owner_onRecordAdded);
     this._owner.RecordDeleted += new EventHandler(this.owner_onRecordDeleted);
     this._flds           = new DbfFieldCollection(this._owner);
     this._flds.Inserted += new CollectionEventHandler(this.flds_onChanged);
     this._flds.Updated  += new CollectionEventHandler(this.flds_onChanged);
     this._flds.Removed  += new CollectionEventHandler(this.flds_onChanged);
     this._flds.Cleared  += new EventHandler(this.flds_onCleared);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Writes a full record to a DBF file using the provided Stream object.  This method assumes the Stream object's write position is located at the position to begin writing.  The record's 'DeletionFlag' value will automatically be set to 0x00.
        /// </summary>
        /// <param name="s">An initialized Stream object of a DBF file with the cursor position set to the beginning of record to be written.</param>
        /// <param name="flds">A DbfFieldCollection detailing the structure of the DBF file being writen to.</param>
        /// <param name="values">An Array.Object value containing the values to write into the DBF record.</param>
        public static void WriteDbfRecord(Stream s, DbfFieldCollection flds, Array values)
        {
            // The first byte of every record is the deletion flag.  This method
            //   assumes the given Stream is at the beginning of the record.
            s.WriteByte(0x00);

            // For each DbfField and value pair, write the data to
            //   the given Stream object.
            for (int i = 0; i < System.Math.Min(flds.Count, values.Length); i++)
            {
                DbfTable.WriteDbfField(s, flds[i], values.GetValue(i));
            }
        }
Exemplo n.º 3
0
        public static DbfTable WriteDbf(DataTable dt, FileStream fs, DbfHeader dbStruct)
        {
            // This string contains all the system data types that map to a
            //   particular DBF data type.  This saves from crazy-long
            //   "if" statements.
            string numericType = "Int16,Int32,Int64,Float,Double,Decimal";

            // This DbfField collection stores the info about the fields
            //   in the DBF file we're creating.
            DbfFieldCollection flds = new DbfFieldCollection();

            #region Write DBF Header
            // The first byte is the version identifier.
            // Here, we're specifying dBase IV.
            fs.WriteByte((byte)DbfHeader.dBaseType.dBase4WithOutMemo);
            // The next three bytes are the date that the file
            //   was last modified.
            DateTime dtNow = DateTime.Now;
            fs.Write(new byte[] { (byte)(dtNow.Year - 1900), (byte)dtNow.Month, (byte)dtNow.Day }, 0, 3);
            // The next four bytes contain a count of the number of records
            //   in the file.  NOTE:  these values are stored in reverse order.
            byte[] recCnt = Hex.GetBytes(Hex.ToHex(dt.Rows.Count).PadLeft(8, '0'));
            Array.Reverse(recCnt);
            fs.Write(recCnt, 0, 4);
            // The next two bytes indicate the offset to the start of the
            //   actual data. NOTE: these values are stored in reverse  order.
            // The base header takes 32 bytes and each field header takes an
            //   additional 32 bytes.
            byte[] hdrSize = Hex.GetBytes(Hex.ToHex(32 + (dt.Columns.Count * 32)).PadLeft(4, '0'));
            Array.Reverse(hdrSize);
            fs.Write(hdrSize, 0, 2);
            // The next two bytes identify the size of each record. This number
            //   represents the sum of all field lengths + 1, due to the first
            //   byte of each record being a deletion flag. NOTE: these values
            //   are stored in reverse order.
            int recLen = 0;
            foreach (DataColumn dc in dt.Columns)
            {
                recLen += (dc.MaxLength > 0) ? dc.MaxLength : 100;
            }
            byte[] recSize = Hex.GetBytes(Hex.ToHex(recLen + 1).PadLeft(4, '0'));
            Array.Reverse(recSize);
            fs.Write(recSize, 0, 2);
            // The next sixteen bytes are not used, so we just fill the
            //   space with zeros.
            byte[] nullBytes = new byte[16]; nullBytes.Initialize();
            fs.Write(nullBytes, 0, 16);
            // The next byte is the compound index flag. This flag should be set to
            //   0x01h if a stuctural CDX file is attached to the database.
            // At present, I don't know how to write or read these, so I will
            //   hardcode this to 0x00h.
            fs.WriteByte(0x00);
            // The next three bytes are also not used, so fill with zeros.
            nullBytes = new byte[3]; nullBytes.Initialize();
            fs.Write(nullBytes, 0, 3);
            #endregion

            #region Write Field Headers
            // Now we parse and write the header for each column.
            int fldOffset = 0;
            if (dbStruct != null)
            {
                flds = dbStruct.Fields;
                byte[] fldsHdr = dbStruct.Fields.GetRawHeader();
                fs.Write(fldsHdr, 0, fldsHdr.Length);
            }
            else
            {
                foreach (DataColumn dc in dt.Columns)
                {
                    DbfHeader.dBaseFieldType fldType;
                    if (dc.DataType.Name == "DateTime")
                    {
                        fldType = DbfHeader.dBaseFieldType.Date;
                    }
                    else if (numericType.Contains(dc.DataType.Name))
                    {
                        fldType = DbfHeader.dBaseFieldType.Numeric;
                    }
                    else if (dc.DataType.Name == "Boolean")
                    {
                        fldType = DbfHeader.dBaseFieldType.Logical;
                    }
                    else
                    {
                        fldType = DbfHeader.dBaseFieldType.Character;
                    }

                    string fldName = dc.ColumnName.Substring(0, (dc.ColumnName.Length <= 10) ? dc.ColumnName.Length : 10);
                    //int fldSize = (dc.MaxLength > 0 || dc.DataType.Name != "String") ? dc.MaxLength : 100;
                    int fldSize = 50;
                    if (dc.DataType.Name == "DateTime")
                    {
                        fldSize = 8;
                    }
                    else if (dc.MaxLength > 0)
                    {
                        fldSize = dc.MaxLength;
                    }

                    flds.Add(new DbfField(fldName, fldType, fldOffset, fldSize, 0, dc.Ordinal));
                    fldOffset += fldSize;
                }
                byte[] fldsHdr = flds.GetRawHeader();
                fs.Write(fldsHdr, 0, fldsHdr.Length);
            }
            #endregion

            #region Write DBF Data
            foreach (DataRow dr in dt.Rows)
            {
                // The first byte of every record is the deletion flag.
                fs.WriteByte(0x00);
                // Convert each value to a byte array & add it to the buffer.
                for (int i = 0; i < dbStruct.Fields.Count; i++)
                {
                    byte[] record = new byte[0];
                    if (flds[i].FieldDataType == DbfHeader.dBaseFieldType.Date)
                    {
                        try
                        { record = Array.ConvertAll <Char, Byte>(((DateTime)dr[i]).ToString("yyyyMMdd").ToCharArray(), new Converter <Char, Byte>(Convert.ToByte)); }
                        catch (InvalidCastException)
                        { record = Array.ConvertAll <Char, Byte>("        ".ToCharArray(), new Converter <Char, Byte>(Convert.ToByte)); }
                    }
                    else if (flds[i].FieldDataType == DbfHeader.dBaseFieldType.Memo)
                    {
                        record = Array.ConvertAll <Char, Byte>("00000000".ToCharArray(), new Converter <char, byte>(Convert.ToByte));
                    }
                    else
                    {
                        record = Array.ConvertAll <Char, Byte>(dr[i].ToString().PadRight(flds[i].FieldLength, ' ').ToCharArray(), new Converter <Char, Byte>(Convert.ToByte));
                    }
                    fs.Write(record, 0, record.Length);
                }
            }
            // Write the EOF markers.
            fs.WriteByte(0x1A); fs.WriteByte(0x1A);
            #endregion

            return(new DbfTable());
        }
Exemplo n.º 4
0
 /// <summary>
 /// Clears this object and releases its resources.
 /// </summary>
 public void Dispose()
 {
     _flds.Clear();
     _flds = null;
 }
Exemplo n.º 5
0
 //***************************************************************************
 // Class Constructors
 //
 public DbfHeader()
 {
     _flds = new DbfFieldCollection();
 }
Exemplo n.º 6
0
        public static DbfTable WriteDbf(DataTable dt, FileStream fs, DbfHeader dbStruct)
        {
            // This string contains all the system data types that map to a
            //   particular DBF data type.  This saves from crazy-long
            //   "if" statements.
            string numericType = "Int16,Int32,Int64,Float,Double,Decimal";

            // This DbfField collection stores the info about the fields
            //   in the DBF file we're creating.
            DbfFieldCollection flds = new DbfFieldCollection();

            #region Write DBF Header
            // The first byte is the version identifier.
            // Here, we're specifying dBase IV.
            fs.WriteByte((byte)DbfHeader.dBaseType.dBase4WithOutMemo);
            // The next three bytes are the date that the file
            //   was last modified.
            DateTime dtNow = DateTime.Now;
            fs.Write(new byte[] { (byte)(dtNow.Year - 1900), (byte)dtNow.Month, (byte)dtNow.Day }, 0, 3);
            // The next four bytes contain a count of the number of records
            //   in the file.  NOTE:  these values are stored in reverse order.
            byte[] recCnt = Hex.GetBytes(Hex.ToHex(dt.Rows.Count).PadLeft(8, '0'));
            Array.Reverse(recCnt);
            fs.Write(recCnt, 0,4);
            // The next two bytes indicate the offset to the start of the
            //   actual data. NOTE: these values are stored in reverse  order.
            // The base header takes 32 bytes and each field header takes an
            //   additional 32 bytes.
            byte[] hdrSize = Hex.GetBytes(Hex.ToHex(32 + (dt.Columns.Count * 32)).PadLeft(4, '0'));
            Array.Reverse(hdrSize);
            fs.Write(hdrSize,0,2);
            // The next two bytes identify the size of each record. This number
            //   represents the sum of all field lengths + 1, due to the first
            //   byte of each record being a deletion flag. NOTE: these values
            //   are stored in reverse order.
            int recLen = 0;
            foreach (DataColumn dc in dt.Columns)
                recLen += (dc.MaxLength > 0) ? dc.MaxLength : 100;
            byte[] recSize = Hex.GetBytes(Hex.ToHex(recLen + 1).PadLeft(4, '0'));
            Array.Reverse(recSize);
            fs.Write(recSize,0,2);
            // The next sixteen bytes are not used, so we just fill the
            //   space with zeros.
            byte[] nullBytes = new byte[16]; nullBytes.Initialize();
            fs.Write(nullBytes,0,16);
            // The next byte is the compound index flag. This flag should be set to
            //   0x01h if a stuctural CDX file is attached to the database.
            // At present, I don't know how to write or read these, so I will
            //   hardcode this to 0x00h.
            fs.WriteByte(0x00);
            // The next three bytes are also not used, so fill with zeros.
            nullBytes = new byte[3]; nullBytes.Initialize();
            fs.Write(nullBytes,0,3);
            #endregion

            #region Write Field Headers
            // Now we parse and write the header for each column.
            int fldOffset = 0;
            if (dbStruct != null)
            {
                flds = dbStruct.Fields;
                byte[] fldsHdr = dbStruct.Fields.GetRawHeader();
                fs.Write(fldsHdr, 0, fldsHdr.Length);
            }
            else
            {
                foreach (DataColumn dc in dt.Columns)
                {
                    DbfHeader.dBaseFieldType fldType;
                    if (dc.DataType.Name == "DateTime")
                        fldType = DbfHeader.dBaseFieldType.Date;
                    else if (numericType.Contains(dc.DataType.Name))
                        fldType = DbfHeader.dBaseFieldType.Numeric;
                    else if (dc.DataType.Name == "Boolean")
                        fldType = DbfHeader.dBaseFieldType.Logical;
                    else
                        fldType = DbfHeader.dBaseFieldType.Character;

                    string fldName = dc.ColumnName.Substring(0, (dc.ColumnName.Length <= 10) ? dc.ColumnName.Length : 10);
                    //int fldSize = (dc.MaxLength > 0 || dc.DataType.Name != "String") ? dc.MaxLength : 100;
                    int fldSize = 50;
                    if (dc.DataType.Name == "DateTime")
                        fldSize = 8;
                    else if (dc.MaxLength > 0)
                        fldSize = dc.MaxLength;

                    flds.Add(new DbfField(fldName, fldType, fldOffset, fldSize, 0, dc.Ordinal));
                    fldOffset += fldSize;
                }
                byte[] fldsHdr = flds.GetRawHeader();
                fs.Write(fldsHdr, 0, fldsHdr.Length);
            }
            #endregion

            #region Write DBF Data
            foreach (DataRow dr in dt.Rows)
            {
                // The first byte of every record is the deletion flag.
                fs.WriteByte(0x00);
                // Convert each value to a byte array & add it to the buffer.
                for (int i = 0; i < dbStruct.Fields.Count; i++)
                {
                    byte[] record = new byte[0];
                    if (flds[i].FieldDataType == DbfHeader.dBaseFieldType.Date)
                    {
                        try
                        { record = Array.ConvertAll<Char, Byte>(((DateTime)dr[i]).ToString("yyyyMMdd").ToCharArray(), new Converter<Char, Byte>(Convert.ToByte)); }
                        catch (InvalidCastException)
                        { record = Array.ConvertAll<Char, Byte>("        ".ToCharArray(), new Converter<Char, Byte>(Convert.ToByte)); }
                    }
                    else if (flds[i].FieldDataType == DbfHeader.dBaseFieldType.Memo)
                    {
                        record = Array.ConvertAll<Char, Byte>("00000000".ToCharArray(), new Converter<char, byte>(Convert.ToByte));
                    }
                    else
                        record = Array.ConvertAll<Char, Byte>(dr[i].ToString().PadRight(flds[i].FieldLength, ' ').ToCharArray(), new Converter<Char, Byte>(Convert.ToByte));
                    fs.Write(record, 0, record.Length);
                }
            }
            // Write the EOF markers.
            fs.WriteByte(0x1A); fs.WriteByte(0x1A);
            #endregion

            return new DbfTable();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Writes a full record to a DBF file using the provided Stream object.  This method assumes the Stream object's write position is located at the position to begin writing.  The record's 'DeletionFlag' value will automatically be set to 0x00.
        /// </summary>
        /// <param name="s">An initialized Stream object of a DBF file with the cursor position set to the beginning of record to be written.</param>
        /// <param name="flds">A DbfFieldCollection detailing the structure of the DBF file being writen to.</param>
        /// <param name="values">An Array.Object value containing the values to write into the DBF record.</param>
        public static void WriteDbfRecord(Stream s, DbfFieldCollection flds, Array values)
        {
            // The first byte of every record is the deletion flag.  This method
            //   assumes the given Stream is at the beginning of the record.
            s.WriteByte(0x00);

            // For each DbfField and value pair, write the data to
            //   the given Stream object.
            for (int i = 0; i < System.Math.Min(flds.Count, values.Length); i++)
                DbfTable.WriteDbfField(s, flds[i], values.GetValue(i));
        }
Exemplo n.º 8
0
 /// <summary>
 /// Clears this object and releases its resources.
 /// </summary>
 public void Dispose()
 {
     _flds.Clear();
     _flds = null;
 }
Exemplo n.º 9
0
 protected internal DbfHeader(DbfTable owner)
 {
     this._owner = owner;
     this._owner.RecordAdded += new EventHandler(this.owner_onRecordAdded);
     this._owner.RecordDeleted += new EventHandler(this.owner_onRecordDeleted);
     this._flds = new DbfFieldCollection(this._owner);
     this._flds.Inserted += new CollectionEventHandler(this.flds_onChanged);
     this._flds.Updated += new CollectionEventHandler(this.flds_onChanged);
     this._flds.Removed += new CollectionEventHandler(this.flds_onChanged);
     this._flds.Cleared += new EventHandler(this.flds_onCleared);
 }
Exemplo n.º 10
0
 //***************************************************************************
 // Class Constructors
 // 
 public DbfHeader()
 {
     _flds = new DbfFieldCollection();
 }