SIGN UP MEMBER LOGIN:    
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
share this article :
post comment
 

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

Do you have Microsoft foxpro driver on this machine ?

Posted by Ankur Gupta Nov 09, 2011

hello , good job , but i have an error that : "ERROR[IM001][Microsoft][ODBC Drive Manager]Drive does not support this function How to fix this : 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"); Thanks for help !

Posted by cyann nam Nov 09, 2011

vevey good

Posted by leo neo Sep 07, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor