Created
November 10, 2015 01:01
-
-
Save xmichaelx/2af096e514899a318e1b to your computer and use it in GitHub Desktop.
dbf importing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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