Click here to Skip to main content
Click here to Skip to main content

DBF2DataSet

, 24 Aug 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
An article for converting DBF files to dataset XML files

Introduction

Hello, there. This is my first contribution to The Code Project; I hope you enjoy it. This is a simple project for converting and merging DBF FoxPro files to an XML file, so you can then load it via Dataset.ReadXml(). I did it because I had a lot of DBF files that I wanted to merge and read in an easier way than with VFP.

Background

In this project, I used the concepts below:

  • DbProviderFactory
  • BackgroundWorker

Using the Code

The project consists of a single form with a local DataSet at the form's level. the form reads a default directory in the config file and then loads the DBF files in that directory in a ListBox. This is the code for filling the ListBox with the DBF files:

private void RefrescarDirectorio(string directorio) 
{
    DirectoryInfo d = new DirectoryInfo(directorio);
    FileInfo[] tablas = d.GetFiles("*.dbf");
    // Cargar dbf's de la ruta

    lstTablas.DataSource = tablas;
    lstTablas.DisplayMember = "Name";
}

Then the users can select the DBF files that they want exported, provide a name for the data table and then click Export. This process can be repeated for any DBF files.

Note that if there exists a data table in the DataSet, the program will try to load the data table with the data in the DBF through Datatable.Load(DataReader). No Exception is thrown if the structure of the data table is different from that of the DBFs to merge; that behavior will not be controlled.

This is the code for filling the dataset with the DBFs selected. It is called from within BackgroundWorker.

DbProviderFactory f = DbProviderFactories.GetFactory("System.Data.OleDb");
DbConnection cn = f.CreateConnection();
cn.ConnectionString = 
    "Provider=vfpoledb.1;Data Source=" + txtRutaDbf.Text + 
    ";Collating Sequence=general;";
cn.Open();
foreach (FileInfo fInfo in lstTablas.SelectedItems) 
{
    string archivo = fInfo.Name;
    string comando = "SELECT * FROM " + archivo;
    DbCommand cm = cn.CreateCommand();
    cm.CommandText = comando;
    try 
    {
        dt.Load(cm.ExecuteReader());
    } 
    catch (Exception) 
    {
        //throw;
    }
}

Points of Interest

It's curious to see how the Load and Merge methods of the DataTable object don't take into account the structure of the current table.

History

  • 2007-08-23 First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

canozurdo
Web Developer
Colombia Colombia
I am Jhonny D Cano. I'm from Colombia, i'm professional in systems engineering. I worked since 1998 in Visual Basic and C++, then in 2.003 I jumped out to .Net development platform (VB.Net) and then i started with C#. I have a solid background in Windows Forms and in my current job I am working with ASP.Net and Web Services over Oracle and SQL Server. Alternatively I got the SUN Certification "Java Certified Programmer" and currently i'm in the process of 70-356's certification .
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberdexterama8-Apr-13 7:53 
GeneralPlease Help Me , I very need it !!!! [modified] Pinmemberhoavan_le5-Nov-07 17:08 
GeneralRe: Please Help Me , I very need it !!!! Pinmembercanozurdo15-Nov-07 3:49 
GeneralJust what I needed Pinmembermarknm18-Oct-07 5:34 
Generalcan't use this PinmemberJannigje at CC10-Oct-07 5:31 
GeneralRe: can't use this Pinmembercanozurdo10-Oct-07 8:11 
GeneralI can't run it Pinmemberhoavan_le5-Nov-07 4:11 
QuestionHow do you save it back as DBF file after the merge? Pinmemberdanielc1268-Sep-07 16:45 
AnswerRe: How do you save it back as DBF Pinmembercanozurdo13-Sep-07 3:46 
GeneralRe: How do you save it back as DBF Pinmemberjcraigue1-Jul-11 12:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 24 Aug 2007
Article Copyright 2007 by canozurdo
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid