ARTICLE

Import Data from FoxPro (DBF) database to DataGridView in C#

Posted by Ankur Gupta Articles | ADO.NET in C# July 23, 2010
In this article you will learn how to Import Data from FoxPro (DBF) database to DataGrid View in C#.
Reader Level:
Download Files:
 

First add a browser control,TextBox,Button and a DataGridView Control on form

Form1.JPG

Add the following code in Button Click Event

        private void button1_Click_1(object sender, EventArgs e)

        {

            OpenFileDialog fdlg = new OpenFileDialog();

            fdlg.Title = "Select file";

            fdlg.InitialDirectory = @"c:\";

            fdlg.FileName = txtFileName.Text;

            fdlg.Filter = "DBF Files(*.dbf)|*.dbf|All Files(*.*)|*.*";

            fdlg.FilterIndex = 1;

            fdlg.RestoreDirectory = true;

            if (fdlg.ShowDialog() == DialogResult.OK)

            {

                txtFileName.Text = fdlg.FileName;

                Import();

                Application.DoEvents();

            }

        }


Form2.JPG

When you select any DBF file from Browser page then you get the full path of dbf file.

You will pass this path in the following function that will return the dbf file data in a dataset.

        public static DataTable GetDataTableDBF(string strFileName)

        {

            System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection("Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + System.IO.Path.GetFullPath(strFileName).Replace(System.IO.Path.GetFileName(strFileName), "") + ";Exclusive=No");

            conn.Open();

            string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";

            System.Data.Odbc.OdbcDataAdapter adapter = new System.Data.Odbc.OdbcDataAdapter(strQuery, conn);

            System.Data.DataSet ds = new System.Data.DataSet();

            adapter.Fill(ds);

            return ds.Tables[0];

        }


You can easily show dataset data in Datagridview.

DataTable
dt = GetDataTableDBF(txtFileName.Text);
dataGridView1.DataSource = dt.DefaultView;

Form3.JPG

Login to add your contents and source code to this article
post comment
     

you the best

Posted by vvinnel shad May 19, 2013

nice post. Thanks alot.

Posted by Eng Kok Heng Jun 09, 2012

nice pose. Thanks alot.

Posted by Eng Kok Heng Jun 09, 2012

ya sure.... My Gamil Account is ankurmee@gmail.com

Posted by Ankur Gupta Nov 09, 2011

no , can you send me your account yahoo or email , i have some problems about files .dbf . Best regard !

Posted by cyann nam Nov 09, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts