Click here to Skip to main content
15,896,154 members
Articles / Programming Languages / C#

Load a DBF into a DataTable

Rate me:
Please Sign up or sign in to vote.
4.80/5 (72 votes)
11 Mar 2008CPOL6 min read 403.4K   8.3K   87  
Load a DBF into a DataTable without using the Jet or other OLE Db drivers
In this post, you will find a simple class to read an entire DBF into a DataTable. From there, you can use .NET to manipulate the data. You will not find any method to query the data; this loads the entire file and that's it. You probably don't want to use this if you have a DBF with tens of thousands of records or more.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.Data;

namespace LoadDBF
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid grdDBF;
		private System.Windows.Forms.Button btnLoadDBF;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.grdDBF = new System.Windows.Forms.DataGrid();
			this.btnLoadDBF = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.grdDBF)).BeginInit();
			this.SuspendLayout();
			// 
			// grdDBF
			// 
			this.grdDBF.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.grdDBF.DataMember = "";
			this.grdDBF.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.grdDBF.Location = new System.Drawing.Point(16, 88);
			this.grdDBF.Name = "grdDBF";
			this.grdDBF.Size = new System.Drawing.Size(864, 464);
			this.grdDBF.TabIndex = 0;
			// 
			// btnLoadDBF
			// 
			this.btnLoadDBF.Location = new System.Drawing.Point(24, 24);
			this.btnLoadDBF.Name = "btnLoadDBF";
			this.btnLoadDBF.Size = new System.Drawing.Size(120, 40);
			this.btnLoadDBF.TabIndex = 1;
			this.btnLoadDBF.Text = "Load DBF";
			this.btnLoadDBF.Click += new System.EventHandler(this.btnLoadDBF_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(896, 566);
			this.Controls.Add(this.btnLoadDBF);
			this.Controls.Add(this.grdDBF);
			this.Name = "Form1";
			this.Text = "Load DBF";
			((System.ComponentModel.ISupportInitialize)(this.grdDBF)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void btnLoadDBF_Click(object sender, System.EventArgs e)
		{
			try
			{
				OpenFileDialog ofd = new OpenFileDialog();
				ofd.Filter = "dBASE files (*.dbf)|*.dbf";
				ofd.ShowDialog();

				if (ofd.FileName.Length > 0)
				{
					DataTable dt = ParseDBF.ReadDBF(ofd.FileName);
					grdDBF.DataSource = dt;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, ex.Message + "\r\r" + ex.StackTrace, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect Kudzu Interactive
United States United States
Brian Duke is the VP of Tech Ops for iHealth Technologies and enjoys staying active in development whenever he can.

Comments and Discussions