private void BtnOpenInitial_Click(object sender, EventArgs e) { OpenFileDialog file = new OpenFileDialog(); if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string path = file.FileName; DTable dbf = new DTable(); dbf.Load(path); if (dbf.Table.Rows.Count > 0) { this.GrcInitialData.DataSource = dbf.Table; _table = new DataTable(); _table = dbf.Table; } } } /// <summary> /// 从硬盘读取dbf文件 /// </summary> /// <param name="filepath">路径</param> /// <returns></returns> public int Load(String filepath) { FileInfo FFile = new FileInfo(filepath); if (FFile.Exists) { Stream fStream = new BufferedStream(new FileStream(filepath, FileMode.Open));//main.dbf try { BinaryReader bReader = new BinaryReader(fStream); //ByteDump(fStream); return ReadDBF(bReader); } catch { ClearDataTable(); return -1; } finally { fStream.Close(); } } else { return -1; } } /// <summary> /// 读取dbf /// </summary> /// <param name="bReader"></param> /// <returns></returns> private int ReadDBF(BinaryReader bReader) { if (ReadHeader(bReader) == 0) { if (ReadFieldDesc(bReader) == 0) { return readRecord(bReader); } else return -1; } else { return -1; } } /// <summary> /// 读取记录 /// </summary> /// <param name="bReader"></param> /// <returns></returns> private int readRecord(BinaryReader bReader) { try { int iRecCount = _hearder.RecordCount; for (int i = 0; i < iRecCount; i++) { DRow row = new DRow(); DataRow datarow = _table.NewRow(); row.RowIndex = i; byte bRecordFlag = bReader.ReadByte(); if (bRecordFlag == 0x20) { row.DeleteFlag = false; } else if (bRecordFlag == 0x2a) { row.DeleteFlag = true; } else { ClearDataTable(); return -1; } for (short j = 0; j < _columns.ColumnCount; j++) { DColumn col = _columns.GetColumnByIndex(j); if (col != null) { DField field = new DField(); field.Column = col; byte[] bValue = bReader.ReadBytes(col.ColumnLength); field.FieldValue = Encoding.Default.GetString(bValue); row.AddField(field); UpdateRowField(datarow, field); } else { return -1; } } _rows.AddRow(row); if (!row.DeleteFlag) _table.Rows.Add(datarow); } //if (_hearder.RecordCount > 0) //{ // byte endflag = bReader.ReadByte(); // if (endflag != 0x1A) // { // ClearDataTable(); // return -1; // } //} return 0; } catch { ClearDataTable(); return -1; } }