Here, in this blog, we will read the DBF file using OleDbConnection and will store the data in two tables.
In the first table, we store the columns name and data type containing the DBF file and in the second table, we store the added data.
Use the below namespace.
- using System;
- using System.IO;
- using System.Data;
- using System.Data.OleDb;
- using System.Collections.Generic;
Declare the given variables.
- DataTable dtData, dtColumn;
- string rfileName = @"E:\AMIT\DBF_Files\test.dbf";
- string conString = "Data Source=.; Initial Catalog=myDataBase; Integrated Security=SSPI";
- public IList<DbfFieldDescriptor> FieldDescriptors { get; set; }
On page load, we have to read the columns and data contained in the DBF file using OleDbConnection.
- private void Form1_Load(object sender, EventArgs e)
- {
- string filePath = Path.GetDirectoryName(rfileName);
- OleDbConnection connection = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=" + filePath + ";");
- connection.Open();
- DataTable tables = connection.GetSchema(OleDbMetaDataCollectionNames.Tables);
- dtColumn = null;
- string fName = Path.GetFileNameWithoutExtension(rfileName);
- foreach (DataRow rowTables in tables.Rows)
- {
- if (rowTables["table_name"].ToString().ToUpper() == fName.ToUpper())
- {
- DataTable columns = connection.GetSchema(OleDbMetaDataCollectionNames.Columns,
- new String[] { null, null, rowTables["table_name"].ToString(), null });
- dtColumn = GetColumnDataTable();
- foreach (System.Data.DataRow rowColumns in columns.Rows)
- {
- DataRow dr = dtColumn.NewRow();
- dr[0] = rowColumns["column_name"].ToString();
- dr[1] = OleDbType(int.Parse(rowColumns["data_type"].ToString()));
- dr[2] = rowColumns["data_type"].ToString();
- dr[3] = rowColumns["numeric_precision"].ToString();
- dtColumn.Rows.Add(dr);
- }
- break;
- }
- }
- string sql = "SELECT * FROM " + fName;
- OleDbCommand cmd = new OleDbCommand(sql, connection);
- OleDbDataAdapter DA = new OleDbDataAdapter(cmd);
- dtData = new DataTable();
- DA.Fill(dtData);
- connection.Close();
- WriteDataToDatabase(dtData, dtColumn);
- }
- // In this method we have create the table that will be stored the columns name and datatype.
- static DataTable GetColumnDataTable()
- {
- DataTable table = new DataTable();
- table.Columns.Add("NAME", typeof(string));
- table.Columns.Add("TYPE", typeof(string));
- table.Columns.Add("TYPENO", typeof(string));
- table.Columns.Add("DEC", typeof(string));
- return table;
- }
- // This method return the datatype name.
- public string OleDbType(int type)
- {
- string dataType;
- switch (type)
- {
- case 10:
- dataType = "BigInt";
- break;
- case 128:
- dataType = "Byte";
- break;
- case 11:
- dataType = "Boolean";
- break;
- case 8:
- dataType = "String";
- break;
- case 129:
- dataType = "String";
- break;
- case 6:
- dataType = "Currency";
- break;
- case 7:
- dataType = "DateTime";
- break;
- case 133:
- dataType = "DateTime";
- break;
- case 134:
- dataType = "TimeSpan";
- break;
- case 135:
- dataType = "DateTime";
- break;
- case 14:
- dataType = "Decimal";
- break;
- case 5:
- dataType = "Double";
- break;
- case 3:
- dataType = "Integer";
- break;
- case 201:
- dataType = "String";
- break;
- case 203:
- dataType = "String";
- break;
- case 204:
- dataType = "Byte";
- break;
- case 200:
- dataType = "String";
- break;
- case 139:
- dataType = "Decimal";
- break;
- case 202:
- dataType = "String";
- break;
- case 130:
- dataType = "String";
- break;
- case 131:
- dataType = "Decimal";
- break;
- case 64:
- dataType = "DateTime";
- break;
- default:
- dataType = "";
- break;
- }
- return dataType;
- }

Chris WPosted Jun 4, 2021, 7:42 AM
How does this translate to VB? "DataTable columns = connection.GetSchema(OleDbMetaDataCollectionNames.Columns, new String[] { null, null, rowTables["table_name"].ToString(), null })"
usha lekkalaPosted Jan 28, 2020, 11:28 AM
I have no idea about dbf file.. my question is i want to generate dbf file from sql table in windows.
usha lekkalaPosted Jan 27, 2020, 5:01 PM
Can you please help me how to generate dbf file based on table data in windows.
usha lekkalaPosted Jan 27, 2020, 5:01 PM
Can you please help me how to generate dbf file based on table data.
Laxmidhar SahooPosted Mar 23, 2019, 7:32 AM
Thanks a lot