创建SQL表中的记录.dbf文件 [英] Create .DBF file from SQL table records
问题描述
我要创建SQL表记录 .DBF
文件。
比如,如果有一个命名表 CountryMaster
在SQL,它有5列:
- ID INT标识( 1,1)
- 名称为varchar(100)
- 详细VARCHAR(200)
- 状态位
- CreatedDate日期时间
和它有100行。
我如何导出这些记录与头从C# .DBF
文件?
注意:创建 .DBF
文件大小必须非常紧凑
您可以看到的Xbase数据文件(* .DBF)的结构和编写自己的代码,但我已经完成了实施,并已使用了好几年。在这里,你可以找到它的 GitHub的
如何使用库
有一些的写在一个名为文件的方法DbfFile的.cs 。您可以使用其中任何一个。我来解释一下其中的一些:
第一次写入方式
保存数据表
为 DBF 文件:
静态无效的写(字符串文件名,系统.Data.DataTable表,编码编码)
-
文件名
:是你想要的的.dbf
输出文件的保存位置。 -
表
:是你们从在 SQL Server的或任何其他源读取数据。 -
编码
:保存字符串数据时使用的编码
第二Write方法
保存列表< T>
成DBF 。文件
静态无效写< T>(字符串文件名,
名单< T>值
清单< Func键< T,对象>>的映射,
名单< DbfFieldDescriptor>列,
编码的编码)
读取数据库,并将结果保存到某个类类型,然后使用这个方法类值保存到DBF文件。这里是它描述的参数:
-
文件名
:DBF文件名是保存 -
值
:您的类型的对象的列表数据T
要保存到一个DBF文件 -
映射
:那告诉这个方法如何从类类型的数据功能列表。 -
列
:DBF列信息 -
编码
:DBF文件的编码
例为第二Write方法
由于第一种方法是直线前进,我为你提供与例如在第二write方法。考虑一下你要一个列表与下保存; MyClass的>
数据转换为DBF文件。以下是代码
MyClass类
{
公众诠释标识{获取;集;}
公共字符串名称{;设置;}
}
现在你可以节省列表< MyClass的>
成这样的DBF文件:
VAR idColumn = DbfFieldDescriptors.GetIntegerField(ID);
变种名称列= DbfFieldDescriptors.GetStringField(名称);
VAR列=新的List< DbfFieldDescriptor>(){idColumn,名称列};
Func键< MyClass的,对象>的azazaz = MyClass的=> myClass.Id;
Func键< MyClass的,对象> MAPNAME = MyClass的=> myClass.Name;
变种映射=新的List<&Func键LT; MyClass的,对象>>(){的azazaz,MAPNAME};
名单,LT; MyClass的>值=新的List< MyClass的>();
values.Add(新MyClass的(){ID = 1,名称=名称1});
DbfFileFormat.Write(@C:\yourFile.dbf,价值观,测绘,列,Encoding.ASCII);
而且使用此图书馆可以阅读DBF文件和你的代码没有
依赖于Microsoft.Jet.OLEDB
或其他任何东西。
块引用>
享受它。
I want to create a
.DBF
file from SQL table records.Such as if there is a table named
CountryMaster
in SQL and it has 5 columns:
- ID int identity(1,1)
- Name varchar(100)
- Details varchar(200)
- Status bit
- CreatedDate datetime
And it has 100 rows.
How can I export these records with headers in
.DBF
file from C#?NOTE: Created
.DBF
file size must be very compact.解决方案You can see the Xbase Data file (*.dbf) structure and write your own code but I have done the implementation and have been using it for years. Here you can find it on GitHub
How to use the library
There are some write methods in a file named DbfFile.cs. You may use any of them. I will explain some of them:
The First Write Method
Save a
DataTable
as dbf file:static void Write(string fileName, System.Data.DataTable table, Encoding encoding)
fileName
: is the location which you want the.dbf
output file be saved.table
: is your data which you have read from the SQL Server or any other source.encoding
: the encoding to be used when saving the string dataThe Second Write Method
Save a
List<T>
into a dbf file.static void Write<T>(string fileName, List<T> values, List<Func<T, object>> mapping, List<DbfFieldDescriptor> columns, Encoding encoding)
Read the database and save the result into some class type then save class value to dbf file using this method. Here is description of it's parameters:
fileName
: the dbf file name to be savedvalues
: Your data as a List of objects of typeT
to be saved into a dbf filemapping
: A list of functions that tell this method how to retrieve data from the class type.columns
: dbf column informationencoding
: the encoding of the dbf file.Example for the Second Write Method
As the first approach is straight forward, I provide you with and example on the second write method. Consider you want to save a
List<MyClass>
data into a dbf file. Here is the codeclass MyClass { public int Id {get;set;} public string Name {get;set;} }
Now you can save a
List<MyClass>
into a dbf file like this:var idColumn = DbfFieldDescriptors.GetIntegerField("Id"); var nameColumn = DbfFieldDescriptors.GetStringField("Name"); var columns = new List<DbfFieldDescriptor>() { idColumn, nameColumn }; Func<MyClass, object> mapId = myClass => myClass.Id; Func<MyClass, object> mapName = myClass => myClass.Name; var mapping = new List<Func<MyClass, object>>() { mapId, mapName }; List<MyClass> values = new List<MyClass>(); values.Add(new MyClass() { Id = 1, Name = "name1" }); DbfFileFormat.Write(@"C:\yourFile.dbf", values, mapping, columns, Encoding.ASCII);
Also using this library you can read dbf files and your code do not depend on
Microsoft.Jet.OLEDB
or anything else.enjoy it.
这篇关于创建SQL表中的记录.dbf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!