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

三世轮回 提交于 2019-11-29 02:08:20

Download Microsoft OLE DB Provider for Visual FoxPro 9.0 and use:

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:

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.

Here is an example using the VFP OleDb provider:

    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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!