public DataTable GetTable()
        {
            using (FileStream fs = new FileStream(this._file, FileMode.Open, FileAccess.Read))
            {
                // Create a datatable to store all the data we retrieve.
                DataTable dtRet = DbfTable.LoadSchema(this);
                dtRet.TableName = this.TableName;

                // Now we read the actual data into the table.
                // First, we skip the header.
                fs.Read(new byte[_hdr.DataOffset], 0, _hdr.DataOffset);

                // Then, we start reading each row into the DataTable
                for (int t = 0; t < _hdr.RecordCount; t++)
                {
                    DataRow dr = dtRet.NewRow();
                    try
                    { dr.ItemArray = this.GetRowData(fs); }
                    catch (Exception ex)
                    {
                        dr.RowError += ((dr.RowError.Length > 0) ? "  " : "") + ex.Message;
                    }
                    // Add the row to the table.
                    dtRet.Rows.Add(dr);
                }

                // Close the data stream
                fs.Close();

                // Return the DataTable
                return(dtRet);
            }
        }
        public void UpdateRecord(long row, Array values)
        {
            using (FileStream fs = new FileStream(this._file, FileMode.Open, FileAccess.ReadWrite))
            {
                // If row '-1' was passed, it means insert, so move the cursor
                //   to the end of the stream.  Otherwise, move it
                //   to the selected record number.
                if (row == -1)
                {
                    fs.Position = fs.Length;
                }
                else
                {
                    this.MoveStreamToRow(row, fs);
                }

                // Write the row data using the static method.
                DbfTable.WriteDbfRecord(fs, this._hdr.Fields, values);

                // If we added a record, we have to update the header to reflect this.
                DbfTable.UpdateRecordCount(fs, this._hdr.RecordCount + 1);
                this.OnRecordAdded(EventArgs.Empty);

                // And set the last update time.
                DbfTable.SetLastUpdateTime(fs);
            }
        }
        public void DeleteRecord(long row)
        {
            using (FileStream srcDbf = new FileStream(this._file, FileMode.Open, FileAccess.Read))
                using (BinaryReader br = new BinaryReader(srcDbf))
                    using (FileStream fs = new FileStream(Path.ChangeExtension(this._file, ".tmpDbf"), FileMode.Create, FileAccess.Write))
                    {
                        // Copy the header data from
                        fs.Write(br.ReadBytes(this._hdr.DataOffset - 2), 0, this._hdr.DataOffset - 2);

                        // Copy all records prior to the one to be deleted.
                        fs.Write(br.ReadBytes((int)((_hdr.RecordSize + 1) * row)), 0, (int)((_hdr.RecordSize + 1) * row));

                        // Now, skip the row to be deleted...
                        br.ReadBytes(_hdr.RecordSize);

                        // ...And write the rest of the file.
                        fs.Write(br.ReadBytes((int)(br.BaseStream.Length - br.BaseStream.Position)), 0, (int)(br.BaseStream.Length - br.BaseStream.Position));

                        // Don't forget to update the record count in the header.
                        DbfTable.UpdateRecordCount(fs, this._hdr.RecordCount - 1);
                        this.OnRecordDeleted(EventArgs.Empty);

                        // And set the last update time.
                        DbfTable.SetLastUpdateTime(fs);
                    }
            // Then move the 'tmpDbf' file to replace the existing DBF file.
            File.Move(this._file, Path.ChangeExtension(this._file, ".rsbak_" + DateTime.Now.ToString("yyyyMMddHHmmss")));
            File.Move(Path.ChangeExtension(this._file, ".tmpDbf"), this._file);
        }
        public DataRow GetRow(int row)
        {
            try
            {
                using (DataTable dtSchema = DbfTable.LoadSchema(this))
                    using (FileStream fs = new FileStream(this._file, FileMode.Open, FileAccess.Read))
                        using (BinaryReader sr = new BinaryReader(fs))
                        {
                            // First we move the pointer to the start of the selected row.
                            this.MoveStreamToRow(row, fs);

                            // Now we're ready to read the data, so prepare
                            //   the DataRow object to return.
                            DataRow retVal = dtSchema.NewRow();

                            // Now we actually read the data.
                            retVal.ItemArray = this.GetRowData(fs);

                            sr.Close();
                            fs.Close();

                            // And return the value.
                            return(retVal);
                        }
            }
            catch
            { throw; }
        }
        public void UpdateColumn(int col, Array values)
        {
            using (FileStream fs = new FileStream(this._file, FileMode.Open, FileAccess.ReadWrite))
            {
                // Move to the start of the first row.
                this.MoveStreamToRow(0, fs);

                // Jump to the start of the field.
                this.MoveStreamToField(col, fs);

                // Then, write each value and then skip the size of a full
                //   record, minus the updated field length, which should
                //   put the Stream's position at the start of that same
                //   field for the next record.
                int recSize = (this._hdr.RecordSize + 1) - this._hdr.Fields[col].FieldLength;
                foreach (object obj in values)
                {
                    DbfTable.WriteDbfField(fs, this._hdr.Fields[col], obj);
                    // If the total numer of bytes read does not match the determined
                    //   number of bytes to skip for each record, it means we hit
                    //   the end of the file.
                    if (fs.Read(new byte[recSize], 0, recSize) != recSize)
                    {
                        break;
                    }
                }

                // And set the LastUpdateDate value in the header.
                DbfTable.SetLastUpdateTime(fs);
            }
        }
Example #6
0
 //***************************************************************************
 // Class Constructors
 // 
 public DbfField(byte[] fldName, byte fldType, byte[] recOffset, int fldLength, int decLength, int ordinal)
 {
     this.FieldNameHex = Hex.ToHex(fldName);
     this.FieldTypeHex = Hex.ToHex(new byte[] { (!string.IsNullOrEmpty(Enum.GetName(typeof(DbfHeader.dBaseFieldType), fldType)) ? fldType : (byte)0) });
     this.RecordOffsetHex = Hex.ToHex(recOffset);
     this.FieldLength = fldLength;
     this.DecimalLength = decLength;
     this.Ordinal = ordinal;
     this.Owner = null;
 }
 public DbfField(string fldName, DbfHeader.dBaseFieldType fldType, int recOffset, int fldLength, int decLength, int ordinal)
 {
     this.FieldNameHex    = Hex.ToHex(Encoding.Unicode.GetBytes(fldName));
     this.FieldTypeHex    = Hex.ToHex(new byte[] { (byte)fldType });
     this.RecordOffsetHex = Hex.ToHex(recOffset).PadLeft(8, '0');
     this.FieldLength     = fldLength;
     this.DecimalLength   = decLength;
     this.Ordinal         = ordinal;
     this.Owner           = null;
 }
 //***************************************************************************
 // Class Constructors
 //
 public DbfField(byte[] fldName, byte fldType, byte[] recOffset, int fldLength, int decLength, int ordinal)
 {
     this.FieldNameHex    = Hex.ToHex(fldName);
     this.FieldTypeHex    = Hex.ToHex(new byte[] { (!string.IsNullOrEmpty(Enum.GetName(typeof(DbfHeader.dBaseFieldType), fldType)) ? fldType : (byte)0) });
     this.RecordOffsetHex = Hex.ToHex(recOffset);
     this.FieldLength     = fldLength;
     this.DecimalLength   = decLength;
     this.Ordinal         = ordinal;
     this.Owner           = null;
 }
 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);
 }
Example #10
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));
            }
        }
Example #11
0
        public void UpdateValue(long row, int col, object value)
        {
            using (FileStream fs = new FileStream(this._file, FileMode.Open, FileAccess.ReadWrite))
            {
                // Move to the start of the selected row.
                this.MoveStreamToRow(row, fs);

                // Move stream to selected field.
                this.MoveStreamToField(col, fs);

                // Write the data using the static method.
                DbfTable.WriteDbfField(fs, this._hdr.Fields[col], value);
            }
        }
Example #12
0
        /// <summary>
        /// Writes a single field of data 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.
        /// </summary>
        /// <param name="s">An initialized Stream object of a DBF file with the cursor position set to the beginning of the field to be written.</param>
        /// <param name="fld">A DbfField object detailing the structure of the field to be written to.</param>
        /// <param name="value">A System.Object value whose string representation will be written to the Stream.</param>
        public static void WriteDbfField(Stream s, DbfField fld, object value)
        {
            // Save the Stream's current position and the size of the field.
            long sPos = s.Position, sLen = fld.FieldLength + fld.DecimalLength;

            // Get the string value of the passed Object 'value' to be written.
            string val = string.Empty;

            if (value != null)
            {
                val = (value.GetType().Name == "DateTime") ? ((DateTime)value).ToString("yyyyMMdd") : value.ToString();
            }

            // Create a byte array from the string representation of 'value'.
            byte[] buffer = Encoding.ASCII.GetBytes(val.Substring(0, System.Math.Min(fld.FieldLength, val.Length)).PadRight(fld.FieldLength, ' '));

            // If the passed Stream object is a FileStream object,
            //   then lock the bytes we're about to write to.
            if (s.GetType().FullName == "System.IO.FileStream")
            {
                ((FileStream)s).Lock(sPos, sLen);
            }

            // Write the byte buffer.  If an error occurs, just throw it back
            //   to the calling code, but don't forget to unlock the stream.
            try
            {
                s.Write(buffer, 0, buffer.Length);
                DbfTable.SetLastUpdateTime(s);
            }
            catch
            { throw; }
            finally
            {
                if (s.GetType().FullName == "System.IO.FileStream")
                {
                    ((FileStream)s).Unlock(sPos, sLen);
                }
                s.Position = sPos;
            }
        }
Example #13
0
 protected internal DbfFieldCollection(DbfTable owner)
     : this()
 {
     this._owner = owner;
 }
Example #14
0
 public static DataTable LoadSchema(DbfTable dbf)
 {
     return(DbfTable.LoadSchema(dbf.Header));
 }
Example #15
0
 public static void SetLastUpdateTime(Stream s)
 {
     DbfTable.SetLastUpdateTime(s, DateTime.Now);
 }
Example #16
0
 public static DbfTable WriteDbf(DataTable dt, string filename, DbfHeader dbStruct)
 {
     using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
         return(DbfTable.WriteDbf(dt, fs, dbStruct));
 }
Example #17
0
 protected internal DbfFieldCollection(DbfTable owner)
     : this()
 {
     this._owner = owner;
 }
Example #18
0
 //***************************************************************************
 // Static Methods
 //
 /// <summary>
 /// Returns the System.Type equivalent to the supplied DbfHeader.dBaseFieldType.
 /// </summary>
 /// <param name="fld">A DbfField object with an initialized DbfHeader.dBaseFieldType value.</param>
 /// <returns>An equivalent System.Type object.</returns>
 public static Type GetSystemDataType(DbfField fld)
 {
     return(DbfTable.GetSystemDataType(fld.FieldDataType));
 }
Example #19
0
 public static DataTable LoadSchema(DbfTable dbf)
 {
     return DbfTable.LoadSchema(dbf.Header);
 }
Example #20
0
 public DbfField(string fldName, DbfHeader.dBaseFieldType fldType, int recOffset, int fldLength, int decLength, int ordinal)
 {
     this.FieldNameHex = Hex.ToHex(Encoding.Unicode.GetBytes(fldName));
     this.FieldTypeHex = Hex.ToHex(new byte[] { (byte)fldType });
     this.RecordOffsetHex = Hex.ToHex(recOffset).PadLeft(8, '0');
     this.FieldLength = fldLength;
     this.DecimalLength = decLength;
     this.Ordinal = ordinal;
     this.Owner = null;
 }
Example #21
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);
 }