使用 c# 获取 .dbf 文件中的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11356878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get data in a .dbf file using c#
提问by user1484319
How can I get the data in a .dbf file using c#??
如何使用 c# 获取 .dbf 文件中的数据?
What I want to do is to read the data in each row (same column) to further process them.
我想要做的是读取每一行(同一列)中的数据以进一步处理它们。
Thanks.
谢谢。
采纳答案by Habib
You may create a connection string to dbf file, then using OleDb, you can populate a dataset, something like:
您可以创建一个到 dbf 文件的连接字符串,然后使用 OleDb,您可以填充数据集,例如:
string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=directoryPath;Extended Properties=dBASE IV;User ID=Admin;Password=;";
using (OleDbConnection con = new OleDbConnection(constr))
{
var sql = "select * from " + fileName;
OleDbCommand cmd = new OleDbCommand(sql, con);
con.Open();
DataSet ds = new DataSet(); ;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
}
Later you can use the ds.Tables[0]for further processing.
稍后您可以使用ds.Tables[0]进行进一步处理。
You may also check this article Load a DBF into a DataTable
您还可以查看这篇文章将 DBF 加载到数据表中
回答by Alexis Leclerc
I found out the accepted answer didn't work for me, as the .dbf files I'm working with are nested in a hierarchy of directories that makes the paths rather long, which, sadly, cause the OleDbCommandobject to throw.
我发现接受的答案对我不起作用,因为我正在使用的 .dbf 文件嵌套在目录层次结构中,这使得路径相当长,可悲的是,这会导致OleDbCommand对象抛出。
I found a neat little librarythat only needs a file path to work. Here's a little sample adapted from the examples on its GitHub page:
我找到了一个整洁的小库,只需要一个文件路径即可工作。这是改编自其 GitHub 页面上的示例的小示例:
var file = "C:\Path\To\File.dbf";
using (var dbfDataReader = new DbfDataReader(file))
{
while (dbfDataReader.Read())
{
var foo = Convert.ToString(dbfDataReader["FOO"]);
var bar = Convert.ToInt32(dbfDataReader["BAR"]);
}
}

