我正在尋找如何從.NET C#程序中讀取DataFlex 6.2數據文件的方法。我需要順序只讀訪問沒有特殊功能的表格數據,基本上只是對包含數據的幾個文件進行解碼,然後從中產生一個DataTable
。從C#中的Visual DataFlex 6讀取數據
我知道有像FlexODBC這樣的商業產品,但在我看來這是一個相對簡單的任務。也許有人知道一個免費的替代方案或數據文件結構文檔,這樣我就不需要用十六進制編輯器自己弄清楚它了嗎?
我正在尋找如何從.NET C#程序中讀取DataFlex 6.2數據文件的方法。我需要順序只讀訪問沒有特殊功能的表格數據,基本上只是對包含數據的幾個文件進行解碼,然後從中產生一個DataTable
。從C#中的Visual DataFlex 6讀取數據
我知道有像FlexODBC這樣的商業產品,但在我看來這是一個相對簡單的任務。也許有人知道一個免費的替代方案或數據文件結構文檔,這樣我就不需要用十六進制編輯器自己弄清楚它了嗎?
我最近找到this link。與情侶對錶的檢查,它不是100%確定,但它是一個很好的quidance:
DATAFLEX 2.3B DATAFILE HEADER STRUCTURE
By Peter M. Grillo
MAINSTREAM COMPUTER CONSULTING
Following is the structure of the DataFlex .DAT file for 2.3. Data
Access Corporation has deemed the structure of the .DAT file as
proprietary. The following definition of a 2.3 .DAT file was derived
independently by myself and any problem arising from the use of this
information will be your problem. Please do not call DAC and snivel. Use
at own risk. Please do not upload this to DAC's BBS.
DAC has indicated to me that I can release this information providing I
include the prior disclaimer.
All that aside, this is everything I know about a DataFlex .DAT file.
The overall layout of a 2.3 .DAT file is header, null record and data.
The header contains information about the file definition. Just about
everything you define in DFFILE can be found in the header except for
tag names. It is possible to read the header of a 2.3 .DAT file and the
corresponding .TAG file to produce a perfect .DEF file.
The following show offsets into the header:
(LSB = Least significant byte)
(MSBT = Most significant bit)
DECIMAL HEX DESCRIPTION
01 - 04 00 - 03 HIGHEST RECORD COUNT EVER (LSB FIRST)
09 - 12 08 - 0B RECORD COUNT (LSB FIRST)
13 - 16 0C - 0F MAXIMUM NUMBER OF RECORDS (LSB FIRST)
79 - 80 4E - 4F RECORD LENGTH (LSB FIRST)
89 58 DELETED SPACE (1=REUSED, 0=NOT REUSED)
90 59 NUMBER OF FIELDS
93 5C MULTIUSER REREAD (1=ACTIVE, 0=INACTIVE)
101 64 NUMBER OF FIELDS IN INDEX 1 (MSBT SET 1 IF BATCH)
102-108 65 - 6B FIELD SEGMENTS OF INDEX 1
109 6C NUMBER OF FIELDS IN INDEX 2 (MSBT SET 1 IF BATCH)
110-116 6D - 73 FIELD SEGMENTS OF INDEX 2
117 74 NUMBER OF FIELDS IN INDEX 3 (MSBT SET 1 IF BATCH)
118-124 75 - 7B FIELD SEGMENTS OF INDEX 3
125 7C NUMBER OF FIELDS IN INDEX 4 (MSBT SET 1 IF BATCH)
126-132 7D - 83 FIELD SEGMENTS OF INDEX 4
133 84 NUMBER OF FIELDS IN INDEX 5 (MSBT SET 1 IF BATCH)
134-140 85 - 8B FIELD SEGMENTS OF INDEX 5
141 8C NUMBER OF FIELDS IN INDEX 6 (MSBT SET 1 IF BATCH)
142-148 8D - 93 FIELD SEGMENTS OF INDEX 6
149 94 NUMBER OF FIELDS IN INDEX 7 (MSBT SET 1 IF BATCH)
150-156 95 - 9B FIELD SEGMENTS OF INDEX 7
157 9C NUMBER OF FIELDS IN INDEX 8 (MSBT SET 1 IF BATCH)
158-162 9D - A3 FIELD SEGMENTS OF INDEX 8
163 A4 NUMBER OF FIELDS IN INDEX 9 (MSBT SET 1 IF BATCH)
164-170 A5 - AB FIELD SEGMENTS OF INDEX 9
171 AC NUMBER OF FIELDS IN INDEX 10 (MSBT SET 1 IF BATCH)
172-108 AD - B3 FIELD SEGMENTS OF INDEX 10
181 -183 B4 - BC FILE ROOT NAME (NULL TERMINATED)
START OF FIELD DEFINITIONS.
REPEAT FOR EACH FIELD.
197-198 C4 - C5 FIELD OFFSET (LSB FIRST)
199 C6 MSBT=MAIN INDEX, LSBT=(DECIMAL POINTS/2)
200 C7 FIELD LENGTH
201 C8 FIELD TYPE 00=ASCII, 01=NUMERIC, 02=DATE, 03=OVERLAP
202 C9 RELATES TO FILE NUMBER
203-204 CA - CB RELATES TO FIELD NUMBER (LSB FIRST)
...-... .. - .. (REPEAT FOR EACH FIELD)
The null record follows the header and usually contains 00h's. The
number of bytes in the null record corresponds to the record length of
the file. The null record is record number zero.
The data that follows are records in order of record number. The number
of bytes in each record corresponds to the record length. Records are
grouped together by blocks of 512 bytes. Not every record length,
however, divides evenly into 512 so you get the occurrence of fill bytes
or 0FFh's to round out a group of records to 512 bytes. Consider the
following:
Record Length Layout
128 Divides into 512 evenly so no fill
bytes are used
170 Divided by 512 is 3 with a remainder
of 2 so after every 3 records
(starting at record 0) the are 2 fill
bytes (0FFh's)
Here is a table of common record lengths:
Record Length Records in 512 Group Number of Fill Bytes
256 2 0
170 3 2
128 4 0
102 5 2
85 6 2
73 7 1
64 8 0
56 9 8
51 10 2
46 11 6
42 12 8
39 13 5
36 14 8
34 15 2
32 16 0
30 17 2
28 18 8
26 19 18
25 20 12
24 21 8
23 22 6
22 23 6
21 24 8
20 25 12
19 26 18
18 28 8
17 30 2
16 32 0
15 34 2
14 36 8
13 39 5
12 42 8
11 46 6
10 51 2
9 56 8
8 64 0
> [fold] [
> [fold] [
Deleted records are filled with 00h's until reused.
DataFlex .DAT files can be opened from .FLX files using DIRECT_INPUT.
You can then use READ_BLOCK commands to read information.
Reading the FILELIST.CFG file is also much more efficient using
DIRECT_INPUT and READ_BLOCK. The first 128 bytes are fill and each
successive block of 128 bytes is a file in the list. In other words, if
you want file 15 then DIRECT_INPUT 'FILELIST.CFG' and READ_BLOCK off
(15*128) bytes. This would point you to the block for file 15. From
there you can read off bytes to find the Root Name, Description, and
DataFlex Name using the following layout.
> [fold] ]
> [fold] ]
DECIMAL HEX DESCRIPTION
01 - 41 00 - 28 FILE ROOT NAME (NULL TERMINATED)
42 - 74 29 - 49 FILE DESCRIPTION (NULL TERMINATED)
75 - 128 4A - 7F DATAFLEX FILE NAME (NULL TERMINATED)
> [fold] 2
在該文件中,我解碼(表3.0)。沒有記錄長度,現場列表啓動在抵消2E0
和記錄之間的差距似乎填充20
而不是00
。此外,記錄不會與512對齊,但記錄大小會增加128. 零記錄從C00
開始。對齊的記錄大小可以像(FileSize - C00)/RecordCount
那樣計算。在A5
有一個字段計數。
至於數據類型:
日期使用3個字節存儲在嵌入式數據庫,BCD格式。這是一個BCD號碼,表示自最後日期以來的天數。 700000
代表1642-09-17
,所以這個數字可以作爲我的基礎。
數字:數字510000001存儲爲15 10 00 00 01
,這在十六進制編輯器中可讀性很好。
因此,這裏是一段代碼來解析DAT文件轉換成數據表C#:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Linq;
namespace DataFlex
{
/// <summary>
/// Classes for parsing DataFlex DAT files version 3.0
/// </summary>
public enum DFFieldType
{
ASCII = 0,
Numeric = 1,
Date = 2,
Overlap = 3,
Unknown = 4
}
public class DFField
{
public DFFieldType Type;
public Type DataType;
public int Position;
public byte Length;
public decimal Precision;
public string Name;
private Byte[] _input;
public DFField(byte[] input, string name)
{
_input = input;
Name = name;
UInt16 helper = BitConverter.ToUInt16(input, 0);
Position = helper;
helper = (ushort)((input[2] & 0x0F) * 2);
if (helper > 0)
Precision = (decimal)Math.Pow(10, helper);
else
Precision = 0;
Length = input[3];
switch (input[4])
{
case 0: Type = DFFieldType.ASCII; DataType = typeof(string); break;
case 1: Type = DFFieldType.Numeric; DataType = typeof(decimal); break;
case 2: Type = DFFieldType.Date; DataType = typeof(DateTime); break;
case 3: Type = DFFieldType.Overlap; DataType = typeof(object); break;
default: Type = DFFieldType.Unknown; break;
}
}
}
public class DFRow
{
public object[] _values;
public DFTable _DFTable;
public object[] Values { get { return _values; } }
public DFRow(byte[] input, DFTable dFTable)
{
_DFTable = dFTable;
_values = new object[dFTable.Fields.Length];
for (int i = 0; i < dFTable.Fields.Length; i++)
{
var f = dFTable.Fields[i];
object o;
switch (f.Type)
{
case DFFieldType.Date: o = BCDToDate(input, f.Position - 1, f.Length); break;
case DFFieldType.Numeric: o = BCDToDecimal(input, f.Precision, f.Position - 1, f.Length, true); break;
default: o = System.Text.Encoding.GetEncoding("ibm852").GetString(input, f.Position - 1, f.Length).TrimEnd(); break;
}
_values[i] = o;
}
}
private decimal BCDToDecimal(byte[] input, decimal precision, int start, int length, bool signed)
{
decimal result = 0;
uint i = 0;
for (i = 0; i < length; i++)
{
if (i > 0 || !signed)
{
result *= 100;
result += (decimal)(10 * (input[start + i] >> 4));
}
else
{
result *= 10;
}
result += (decimal)(input[start + i] & 0xf);
}
if (precision > 0)
result = (result/precision);
return (result);
}
private DateTime? BCDToDate(byte[] input, int start, int length)
{
DateTime baseDate = new DateTime(1642, 09, 14);
decimal baseNumber = 700000;
decimal dn = BCDToDecimal(input, 0, start, length, false);
dn = dn - baseNumber;
DateTime? result = null;
if (dn > 0)
{
result = baseDate.AddDays((double)dn);
}
return result;
}
}
public class DFTable
{
private UInt32 _RecordCount;
private DFField[] _Fields;
private List<DFRow> _Rows;
private UInt16 _RecordLength = 0;
private byte _FieldCount = 0;
private string[] _tags = null;
public DFField[] Fields
{
get { return _Fields; }
}
public List<DFRow> Rows
{
get { return _Rows; }
}
public DFRow LastRecord
{
get { return Rows[Rows.Count-1]; }
}
public DFTable(Stream datStream, bool readLastRecordOnly, string tagFile, string tableName)
{
if (File.Exists(tagFile))
_tags = File.ReadLines(tagFile).ToArray();
//Parsujeme hlavičku
byte[] input = new byte[4];
datStream.Read(input, 0, 4);
_RecordCount = BitConverter.ToUInt32(input, 0);
datStream.Seek(0xA5, SeekOrigin.Begin);
datStream.Read(input, 0, 1);
_FieldCount = input[0];
datStream.Seek(0x2E0, SeekOrigin.Begin);
_Fields = new DFField[_FieldCount];
//Parsujeme strukturu
int i;
for (i = 0; i < _FieldCount; i++)
{
input = new byte[8];
datStream.Read(input, 0, 8);
string name = _tags == null || _tags.Length<=i ? "F" + i.ToString() : _tags[i];
_Fields[i] = (new DFField(input, name));
}
//Parsujeme záznamy
long totalLength = datStream.Length;
_RecordLength = (ushort)((totalLength - 0xC00)/_RecordCount);
_Rows = new List<DFRow>();
input = new byte[_RecordLength];
if (readLastRecordOnly)
{
datStream.Seek(0xC00 + (_RecordCount)*_RecordLength, SeekOrigin.Begin); //Nastavit na poslední
datStream.Read(input, 0, _RecordLength);
_Rows.Add(new DFRow(input, this));
}
else
{
datStream.Seek(0xC00 + _RecordLength, SeekOrigin.Begin); //Nastavit na začátek
for (int row = 0; row < _RecordCount; row ++)
{
datStream.Read(input, 0, _RecordLength);
_Rows.Add(new DFRow(input, this));
}
}
}
/// <summary>
/// Převede na DataTable
/// </summary>
/// <returns></returns>
public DataTable ToDataTable()
{
DataTable dt = new DataTable();
DataColumn dc;
for (int i=0; i< this.Fields.Length; i++)
{
var f = this.Fields[i];
dc = new DataColumn(f.Name, f.DataType);
dt.Columns.Add(dc);
}
//Záznamy od prvního
foreach (var r in this.Rows)
{
DataRow dr = dt.NewRow();
int j = 0;
foreach (object v in r.Values)
{
dr[j] = v ?? DBNull.Value;
j++;
}
dt.Rows.Add(dr);
}
return dt;
}
/// <summary>
/// https://stackoverflow.com/a/4959869/2224701
/// </summary>
/// <param name="dt"></param>
/// <param name="csvFileName"></param>
public void SaveAsCSV(string csvFileName, bool header)
{
StringBuilder sb = new StringBuilder();
if (header)
{
IEnumerable<string> columnNames = this.Fields.
Select(column => column.Name);
sb.AppendLine(string.Join(",", columnNames));
}
foreach (DFRow row in this.Rows)
{
IEnumerable<string> fields = row.Values.Select(field =>
string.Concat("\"", field!=null ? (field is DateTime ? ((DateTime)field).ToShortDateString() : field.ToString()).Replace("\"", "\"\"") : "", "\""));
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText(csvFileName, sb.ToString());
}
}
}
用法是這樣的:
string fileToRead = @"D:\Table.DAT";
MemoryStream msAla = new MemoryStream(File.ReadAllBytes(fileToRead));
DFTable dft = new DFTable(msAla, false, tagFile, tname);
DataTable dt = dft.ToDataTable();
我不知道任何開源庫來做到這一點。如果是「一次性」,您可能需要嘗試「Visual DataPump」,這可以將您的VDF數據庫導出到SQL數據庫中。這不是免費的,但對於小的東西評估版本應該工作(至少60天)。
這不正是我所期待的。我想定期閱讀實時的'.dat'文件。我已經看過文件的內部結構,它看起來很簡單直接。在C#中閱讀'.dbf'文件沒有問題,那麼爲什麼'.dat'文件應該有問題?我只是懶得破譯廢品的格式。 Vielen DankfürIhren Vorschlag,quandmême。 – 2014-10-20 16:00:37
有一個[官方DataFlex支持論壇中的線程](http://support.dataaccess.com/Forums/showthread.php?54784-OT-Reading-and-Writing-to-DataFlex-native-DB-from- NET)關於這個話題。他們建議使用WebService或[CONNX](http://www.connx.com/products/dataflex.html)(另一個用於DataFlex的.NET/ODBC驅動程序)。 – Peanut 2014-10-21 06:02:14
感謝您的鏈接 - 不,我不是交叉發送......可能是地球上DataFlex 6的最後兩個應用程序? – 2014-10-21 06:21:34