/// <summary> /// This appends the content of one datarow to a dBase file. /// </summary> /// <exception cref="ArgumentNullException">The columnValues parameter was null</exception> /// <exception cref="InvalidOperationException">Header records need to be written first.</exception> /// <exception cref="InvalidDataException">Table property of columnValues parameter cannot be null.</exception> public void WriteTable() { if (_dataTable == null) return; // _writer.Write((byte)0x20); // the deleted flag NumberConverter[] ncs = new NumberConverter[_columns.Count]; for (int i = 0; i < _columns.Count; i++) { Field fld = _columns[i]; ncs[i] = new NumberConverter(fld.Length, fld.DecimalCount); } for (int row = 0; row < _dataTable.Rows.Count; row++) { _writer.Write((byte) 0x20); // the deleted flag int len = _recordLength - 1; for (int fld = 0; fld < _columns.Count; fld++) { string name = _columns[fld].ColumnName; object columnValue = _dataTable.Rows[row][name]; if (columnValue == null || columnValue is DBNull) WriteSpaces(_columns[fld].Length); else if (columnValue is decimal) _writer.Write(ncs[fld].ToChar((decimal) columnValue)); else if (columnValue is double) { //Write((double)columnValue, _columns[fld].Length, _columns[fld].DecimalCount); char[] test = ncs[fld].ToChar((double) columnValue); _writer.Write(test); } else if (columnValue is float) { //Write((float)columnValue, _columns[fld].Length, _columns[fld].DecimalCount); Field currentField = _columns[fld]; if (currentField.TypeCharacter == 'F') { string val = ((float) columnValue).ToString(); Write(val, currentField.Length); } else { char[] test = ncs[fld].ToChar((float) columnValue); _writer.Write(test); } } else if (columnValue is int || columnValue is short || columnValue is long || columnValue is byte) Write(Convert.ToInt64(columnValue), _columns[fld].Length, _columns[fld].DecimalCount); else if (columnValue is bool) Write((bool) columnValue); else if (columnValue is string) { int length = _columns[fld].Length; Write((string) columnValue, length); } else if (columnValue is DateTime) WriteDate((DateTime) columnValue); else Write((string) columnValue, _columns[fld].Length); len -= _columns[fld].Length; } // If, for some reason the column lengths don't add up to the total record length, fill with spaces. if (len > 0) WriteSpaces(len); } }
/// <summary> /// saves a single row to the data source. /// </summary> /// <param name="index">the integer row (or FID) index</param> /// <param name="values">The object array holding the new values to store.</param> public virtual void Edit(int index, DataRow values) { int rawRow = GetFileIndex(index); NumberConverter[] ncs = new NumberConverter[_columns.Count]; for (int i = 0; i < _columns.Count; i++) { Field fld = _columns[i]; ncs[i] = new NumberConverter(fld.Length, fld.DecimalCount); } //overridden in sub-classes FileStream myStream = new FileStream(_filename, FileMode.Open, FileAccess.Write, FileShare.Write, 100000); _writer = new BinaryWriter(myStream); myStream.Seek(_headerLength + _recordLength * rawRow, SeekOrigin.Begin); _writer.Write((byte)0x20); // the deleted flag int len = _recordLength - 1; for (int fld = 0; fld < _columns.Count; fld++) { string name = _columns[fld].ColumnName; object columnValue = values[name]; if (columnValue == null || columnValue is DBNull) WriteSpaces(_columns[fld].Length); else if (columnValue is decimal) _writer.Write(ncs[fld].ToChar((decimal)columnValue)); else if (columnValue is double) { //Write((double)columnValue, _columns[fld].Length, _columns[fld].DecimalCount); char[] test = ncs[fld].ToChar((double)columnValue); _writer.Write(test); } else if (columnValue is float) { //Write((float)columnValue, _columns[fld].Length, _columns[fld].DecimalCount); Field currentField = _columns[fld]; if (currentField.TypeCharacter == 'F') { string val = ((float)columnValue).ToString(); Write(val, currentField.Length); } else { char[] test = ncs[fld].ToChar((float)columnValue); _writer.Write(test); } } else if (columnValue is int || columnValue is short || columnValue is long || columnValue is byte) Write(Convert.ToInt64(columnValue), _columns[fld].Length, _columns[fld].DecimalCount); else if (columnValue is bool) Write((bool)columnValue); else if (columnValue is string) { int length = _columns[fld].Length; Write((string)columnValue, length); } else if (columnValue is DateTime) WriteDate((DateTime)columnValue); else Write((string)columnValue, _columns[fld].Length); len -= _columns[fld].Length; } // If, for some reason the column lengths don't add up to the total record length, fill with spaces. if (len > 0) WriteSpaces(len); _writer.Flush(); _writer.Close(); }