Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to write a DBF file from scratch in my program. I want to create it, add some columns, and then add data to the columns X amount of times. 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.

The purpose of this is to make the DBF part of an ESRI ShapeFile.

Does anyone know how to do this?

like image 270
Greg Avatar asked Feb 08 '11 11:02

Greg


People also ask

How do I create a dbf file?

Create DBF file (Ctrl+N) Opens the File Structure dialog box: To create a new DBF file, you need to fill in the following fields: File Name – the full file name, including the path. Click the "..." button to invoke the Save dialog box. Specify the file name and click Save. File Type – the new DBF file type.

How to write class values to dbf file in Java?

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.

How to write to a dbf file using static void?

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:

How to save a list<T> into a dbf file?

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:


2 Answers

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();
}
like image 137
Jaroslav Jandek Avatar answered Oct 06 '22 22:10

Jaroslav Jandek


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();
    }
like image 26
Tom Brothers Avatar answered Oct 06 '22 22:10

Tom Brothers