简体   繁体   English

如何在C#中从头开始创建DBF文件?

[英]How to create a DBF file from scratch in C#?

I am trying to write a DBF file from scratch in my program. 我试图在我的程序中从头开始编写DBF文件。 I want to create it, add some columns, and then add data to the columns X amount of times. 我想创建它,添加一些列,然后将数据添加到X列的次数。 My program will not need to read it in again, but other programs will. 我的程序不需要再读它,但其他程序会。

I've looked around for a solution to this, but all seem to assume an existing DBF file, whereas I want to make a new one. 我四处寻找解决方案,但似乎所有人都假设现有的DBF文件,而我想创建一个新文件。

The purpose of this is to make the DBF part of an ESRI ShapeFile. 这样做的目的是使DBF成为ESRI ShapeFile的一部分。

Does anyone know how to do this? 有谁知道如何做到这一点?

Download Microsoft OLE DB Provider for Visual FoxPro 9.0 and use: 下载用于Visual FoxPro 9.0的Microsoft OLE DB提供程序并使用:

string connectionString = @"Provider=VFPOLEDB.1;Data Source=D:\temp";
using (OleDbConnection connection = new OleDbConnection(connectionString))
using (OleDbCommand command = connection.CreateCommand())
{
    connection.Open();

    OleDbParameter script = new OleDbParameter("script", @"CREATE TABLE Test (Id I, Changed D, Name C(100))");

    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "ExecScript";
    command.Parameters.Add(script);
    command.ExecuteNonQuery();
}

Edit : The OP does not want a FoxPro DBF format but dBase IV format: 编辑 :OP不需要FoxPro DBF格式,但需要dBase IV格式:

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp;Extended Properties=dBase IV";

using (OleDbConnection connection = new OleDbConnection(connectionString))
using (OleDbCommand command = connection.CreateCommand())
{
    connection.Open();

    command.CommandText = "CREATE TABLE Test (Id Integer, Changed Double, Name Text)";
    command.ExecuteNonQuery();
}

I don't know anything about ESRI ShapeFile... but you can create a dbf using OleDb. 我对ESRI ShapeFile一无所知......但你可以使用OleDb创建一个dbf。

Here is an example using the VFP OleDb provider: 以下是使用VFP OleDb提供程序的示例:

    string dbfDirectory = @"c:\";
    string connectionString = "Provider=VFPOLEDB;Data Source=" + dbfDirectory;
    using (OleDbConnection connection = new OleDbConnection(connectionString)) {
        connection.Open();
        OleDbCommand command = connection.CreateCommand();

        command.CommandText = "create table Customer(CustId int, CustName v(250))";
        command.ExecuteNonQuery();
        connection.Close();
    }

If you want to completely eliminate external dependencies, you will have to resort to creating the file manually. 如果要完全消除外部依赖关系,则必须手动创建文件。 And in order to do that, you'll need to spend some quality time with the whitepaper describing the file format's specifications to understand the fields you need to implement and what they should contain. 为了做到这一点,您需要花一些时间在描述文件格式规范的白皮书上,以了解您需要实现的字段以及它们应包含的内容。 You can find that online here: http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf 你可以在这里找到它: http//www.esri.com/library/whitepapers/pdfs/shapefile.pdf

Of course, this isn't really an undertaking for the faint of heart. 当然,这对于胆小的人来说并不是真正的事。 Make sure that you understand the work that is entailed here before embarking on the journey. 在开始旅程之前,请确保您了解此处所需的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM