Skip to content

Instantly share code, notes, and snippets.

@xmichaelx
Created November 10, 2015 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xmichaelx/2af096e514899a318e1b to your computer and use it in GitHub Desktop.
Save xmichaelx/2af096e514899a318e1b to your computer and use it in GitHub Desktop.
dbf importing
using System.Data;
using System.Data.OleDb;
using System.IO;
namespace Importers
{
public class DBFImporter
{
public static DataTable ExtractDataTable(string filePath)
{
string template = "Provider=vfpoledb;Data Source={0};Extended Properties=\"dBase IV\";Locale Identifier=852;";
string connectionString = string.Format(template, Path.GetDirectoryName(filePath));
string query = string.Format("select * FROM {0}", Path.GetFileName(filePath));
DataTable table = new DataTable(Path.GetFileNameWithoutExtension(filePath));
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
using (OleDbCommand command = new OleDbCommand(query, connection))
{
using (OleDbDataReader reader = command.ExecuteReader())
{
for (var i = 0; i < reader.FieldCount; i++)
{
table.Columns.Add(columnName: reader.GetName(i), type: reader.GetFieldType(i));
}
while (reader.Read())
{
var row = table.NewRow();
for (var i = 0; i < reader.FieldCount; i++)
{
row[i] = reader[i];
}
table.Rows.Add(row);
}
}
}
connection.Close();
}
return table;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment