• C# 读取 dbf 文件


     

    C# - 读取 dbf 文件

    内容参考来自

    https://www.bilibili.com/read/cv5535385/

    https://www.cnblogs.com/yzhyingcool/p/10657350.html

    一、使用 Nuget 安装 FastDBF

    安装

    二、编写一个例子

    65001 utf8 编码 936 GB2312 <- 我没记错的话

    dbfFile = new DbfFile(Encoding.GetEncoding(65001));

    dbfFile.Open(filename, System.IO.FileMode.Open);

    DbfRecord recoder = dbfFile.Read(0);

    while (recorder != null) {

        // 读取到的内容需要自行转换为自己要的类型

        // 例如 int.Parse(value);

        string value = recoder[fildName];

        recoder = dbfFile.ReadNext();

    }

    实际上,这个博文中有一个GitHub地址

    https://github.com/SocialExplorer/FastDBF

    NuGet 下载旁边也有一个GitHub地址

    https://github.com/alexxonline/FastDBF

    可以看到 NuGet GitHub 的作者很实诚的说他这个是 fork 来自

    NuGet 地址

    如果需要写dbf文件,请自行阅读官方例子,我只是用到了读而已。

    方法2:

    Link: https://www.cnblogs.com/weekzero/archive/2009/10/13/1582793.html

     

    现在开发的很多软件需要和一些老的系统进行数据交互,其中仍然有很多的在用foxpro数据库,对于我们这些一毕业就是.netSqlServer的环境里,还真有些头疼。

    前些天就遇到一个将dbf数据文件读取到datagridview里,然后再导入到SqlServer数据库里,难点就是如何将dbf文件读取到datasetdatatable里。

    下面是其中的一个方法,就是利用OleDb类来读取,在一些客户的电脑上可能没有读取dbf的组件,这里需要安装一个文件"VFPOLEDBSetup.msi",这个可以到网上搜索,一堆,也可以到微软官方去下载。代码如下:

     

     

                string filePath = textBox2.Text;    //文件路径,如:E:\a.dbf
                FileInfo fi = new FileInfo(filePath);
                string mulu = fi.DirectoryName;
                string filename = fi.Name;

                OleDbConnection conn = new OleDbConnection();
                string table = filePath;

                string connStr = @"Provider=VFPOLEDB.1;Data Source=" + mulu + ";Collating Sequence=MACHINE";

                conn.ConnectionString = connStr;
                conn.Open();

                string sql = @"select * from " + filename;
                OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                da.Fill(dt);

     

  • 相关阅读:
    [Swift]LeetCode910. 最小差值 II | Smallest Range II
    转 关于shell脚本中#!/bin/bash and #!/bin/ksh 的说明
    转 对象继承
    转 PHP编程过程中需要了解的this,self,parent的区别
    转: ORA-06508 could not find program unit being called: "DBSNMP.BSLN_INTERNAL
    Multitenant best Practice clone pdb seed and Clone a Pluggable Database – 12c Edition
    Plugging an Unplugged Pluggable Database issue 3
    日历 php
    Datapatch AND What to do if the status of a datapatch action was not SUCCESS due to finding non-ignorable errors
    oracle中的用户详解 【转】
  • 原文地址:https://www.cnblogs.com/xiexiaokui/p/16133910.html
Copyright © 2020-2023  润新知