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

读取DBF文件的部分代码的更多相关文章

  1. C# 解决读取dbf文件,提示Microsoft Jet 数据库引擎找不到对象的问题

    前言 最新项目需要经常和dbf文件打交道,在实际场景中很多软件需要和一些老的系统进行数据交互,而这些系统都在使用foxpro数据库,读取dbf文件一般都是分为两种情况:第一:安装foxpro的驱动进行 ...

  2. php读取excel文件的实例代码

    php读取excel文件的实例代码. 代码: <?php /** * php读取excel文件 * by www.jbxue.com */ $this->loadexcel();//半酣p ...

  3. python3 读取dbf文件报错 UnicodeDecodeError: 'gbk' codec can't decode

    在读取dbf文件时由于编码问题报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xb5 in position 49: incomplete ...

  4. PHP读取超大文件的实例代码

    数据量大带来的问题就是单个文件很大,能够打开这个文件相当不容易,记事本就不要指望了,果断死机   去年年底的各种网站帐号信息的数据库泄漏,很是给力啊,趁机也下载了几个数据库,准备学学数据分析家来分析一 ...

  5. PHPExcel读取Excel文件的实现代码

    <?php require_once 'PHPExcel.php'; /**对excel里的日期进行格式转化*/ function GetData($val){ $jd = GregorianT ...

  6. (实用篇)PHPExcel读取Excel文件的实现代码

    用PHPExcel读取Excel 2007 或者Excel2003文件,需要的朋友,可以参考下. 涉及知识点:  php对excel文件进行循环读取 php对字符进行ascii编码转化,将字符转为十进 ...

  7. 使用Python读取Dbf文件

    DBF:一种特殊的文件格式!表示数据库文件,Foxbase,Dbase,Visual FoxPro等数据库处理系统所产生的数据库文件! DBF 数据库是常用的桌面型数据库,它曾经被各企业.事业单位广泛 ...

  8. 读取DBF文件数据

    #region 返回DBF表 public static System.Data.DataTable getDTFromDBF(string fullPath) { string pDir = Sys ...

  9. shp系列(六)——利用C++进行Dbf文件的写(创建)

    上一篇介绍了shp文件的创建,接下来介绍dbf的创建. 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 1.Dbf头文件的创建 Dbf头文件的结构 ...

随机推荐

  1. windows git的安装配置(转)

    Win7上Git安装及配置过程 http://www.cnblogs.com/sunny5156/archive/2012/10/23/2735799.html   对于需要使用Putty登录的参见 ...

  2. SQL server数据类型、增删改查

    数据类型: 整数型:bigint.int.smallint.mediumint.tinyint 小数类型:decimal.numeric 浮点型:real.float.double 位型:bit 字符 ...

  3. Linux_常用命令_04_挂载

    1. mount [-t vfstype] [-o options] device dir ZC: -o 后面跟多个option的话,用逗号隔开.(例如:"mount -o rw,remou ...

  4. Linux输入子系统(转)

    Linux输入子系统(Input Subsystem) 1.1.input子系统概述 输入设备(如按键,键盘,触摸屏,鼠标等)是典型的字符设备,其一般的工作机制是低层在按键,触摸等动作发生时产生一个中 ...

  5. mysql 性能问题的解决

    场景:模拟一天的数据,每个10秒,遍历1000个设备,每个设备模拟一个实时数据,总的数据量为:24*60*60/10*1000 = 864万条记录.-------------------------- ...

  6. JavaSE复习_10 多线程复习

    △wait()和sleep()的区别:  1.wait():没有等待时间,而sleep()需要有等待时间作为参数.  2.在同步中对于CPU的执行权和锁的处理不同:   wait()会释放执行权和锁. ...

  7. Java注解Annotation学习

    学习注解Annotation的原理,这篇讲的不错:http://blog.csdn.net/lylwo317/article/details/52163304 先自定义一个运行时注解 @Target( ...

  8. Hashtable HashMap

    Hashtable和HashMap类有三个重要的不同之处.第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现. ...

  9. android设置软键盘搜索键以及监听搜索键点击时发生两次事件的问题解决

    在输入框中加入android:imeOptions="actionSearch",调用软键盘时,回车键就会显示搜索二字. 我想在点击搜索时,跳转到下一个页面,但是调用setOnKe ...

  10. @responseBody注解的使用

    1. @responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML 数 ...