Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / C#
Article

DBF2DataSet

Rate me:
Please Sign up or sign in to vote.
3.46/5 (11 votes)
24 Aug 2007CPOL1 min read 59.8K   1.1K   35   10
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:

C#
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.

C#
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)


Written By
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 .

Comments and Discussions

 
GeneralMy vote of 4 Pin
dexterama8-Apr-13 6:53
professionaldexterama8-Apr-13 6:53 
GeneralPlease Help Me , I very need it !!!! [modified] Pin
hoavan_le5-Nov-07 16:08
hoavan_le5-Nov-07 16:08 
GeneralRe: Please Help Me , I very need it !!!! Pin
canozurdo15-Nov-07 2:49
canozurdo15-Nov-07 2:49 
GeneralJust what I needed Pin
marknm18-Oct-07 4:34
marknm18-Oct-07 4:34 
Generalcan't use this Pin
Jannigje at CC10-Oct-07 4:31
Jannigje at CC10-Oct-07 4:31 
GeneralRe: can't use this Pin
canozurdo10-Oct-07 7:11
canozurdo10-Oct-07 7:11 
GeneralI can't run it Pin
hoavan_le5-Nov-07 3:11
hoavan_le5-Nov-07 3:11 
QuestionHow do you save it back as DBF file after the merge? Pin
danielc1268-Sep-07 15:45
danielc1268-Sep-07 15:45 
AnswerRe: How do you save it back as DBF Pin
canozurdo13-Sep-07 2:46
canozurdo13-Sep-07 2:46 
GeneralRe: How do you save it back as DBF Pin
jcraigue1-Jul-11 11:32
jcraigue1-Jul-11 11:32 

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

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