由于种种原因,需要将某些数据以dbf的形式导出,网上苦寻资料,走了许多弯路;
首先记录下走的弯路
1.DBF字段名称长度限制
在编程过程中老是报一些奇怪的错误,创建表格的sql语句拿到sql数据库中跑完全没有问题,但是数据库中老是报各种奇怪的错误,比如建表语句:
create table 表格(公司本年度总收入 int,公司本年度总支出 int)
在程序中执行这段语句总是会出现错误:“公司本年度”已经存在于表中,百思不得其解;
后来经过查资料,各种死磕,最后发现所有成功导出的dbf文件中所有字段都不超过5个汉字,10个英文字符,这是dbf毫无人性的地方,字段定义不能超过10个字节,如果超过则自动截取,运气好截取的前五个字没有重复就成功导出,脸黑的就。。。
2.DBF字段类型
目前真没有找到DBF字段类型官方详细的介绍,我只能说说我当前用过的有varchar,int,double,char,numeric,至于长度什么的,这个自己下个fvp玩玩就知道了;
废话少说,直接上代码
public class DbfExportHelper
{
/// <summary>
/// 数据库所在路径
/// </summary>
private string filePath = "";
/// <summary>
/// 连接字符串
/// </summary>
private string connstring = "";
/// <summary>
/// 数据库连接
/// </summary>
private OleDbConnection Connection = new OleDbConnection();
/// <summary>