/// <summary> /// Setup before calls to OverwriteDataRow /// </summary> /// <returns></returns> private NumberConverter[] BeginEdit() { 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); return ncs; }
/// <summary> /// Common code for editing an existing row in the data source /// </summary> /// <param name="index"></param> /// <param name="values"></param> /// <param name="ncs"></param> private void OverwriteDataRow(int index, object values, NumberConverter[] ncs) { // We allow passing in either DataRow values or Dictionary, so figure out which Dictionary<string, object> dataDictionary = null; DataRow dataRow = values as DataRow; if (null == dataRow) dataDictionary = values as Dictionary<string, object>; int rawRow = GetFileIndex(index); _writer.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; // ReSharper disable PossibleNullReferenceException object columnValue = dataRow != null ? dataRow[name] : dataDictionary[name]; // ReSharper restore PossibleNullReferenceException 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(columnValue.ToString(), _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> /// 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(columnValue.ToString(), _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> /// 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 var 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]; WriteColumnValue(columnValue, fld, ncs); 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); } }
private void WriteColumnValue(object columnValue, int fld, NumberConverter[] ncs) { 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(columnValue.ToString(), _columns[fld].Length); }
/// <summary> /// Common code for editing an existing row in the data source /// </summary> /// <param name="index"></param> /// <param name="values"></param> /// <param name="ncs"></param> private void OverwriteDataRow(int index, object values, NumberConverter[] ncs) { // We allow passing in either DataRow values or Dictionary, so figure out which Dictionary<string, object> dataDictionary = null; var dataRow = values as DataRow; if (null == dataRow) dataDictionary = (Dictionary<string, object>)values; int rawRow = GetFileIndex(index); _writer.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; var columnValue = dataRow != null ? dataRow[name] : dataDictionary[name]; WriteColumnValue(columnValue, fld, ncs); 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> /// Setup before calls to OverwriteDataRow /// </summary> /// <returns></returns> private NumberConverter[] BeginEdit() { 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 _writer = GetBinaryWriter(); return ncs; }