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 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); } }