In one of my projects I have to read FoxPro DBF-files and import data from them. As this code must run in server and customer doesn’t want to install FoxPro there we found another solution that seems at least to me way better. In this posting I will show you how to read DBF-files using Sybase Advantage data providers.
Getting Advantage data providers
Here are the download links to data providers:
- Advantage .NET Data Provider Release 10.1 for Windows (32-bit and 64-bit)
- Platforms for Advantage OLE DB Provider Release 10.1
- Platforms for Advantage ODBC Driver Release 10.1
I downloaded and installed .NET data provider and my example here is fully based on this.
Configuring application
Error 5185: Local server connections are restricted in this environment. See the 5185 error code documentation for details.
Go to your application bin folder and add there usual text file called ads.ini. Here is the content for this file:
[SETTINGS]
MTIER_LOCAL_CONNECTIONS=1
Make sure you add reference to Advantage data provider assembly and include ads.ini to your project like shown on image above.
Getting data to DataTable
Here is short code example about how to get data from DBF-file to DataTable.
static void Main(string[] args)
{
var tableName = "TABLENAME_WITHOUT_EXTENSION";
var connStr = "data source={0};tabletype=vfp;servertype= local;";
connStr = string.Format(connStr, "c:\\temp\\");
var table = new DataTable();
using (var conn = new AdsConnection(connStr))
using (var adapter = new AdsDataAdapter())
using (var cmd = new AdsCommand())
{
cmd.Connection = conn;
cmd.CommandText = "select * from " + tableName;
adapter.SelectCommand = cmd;
conn.Open();
adapter.Fill(table);
conn.Close();
}
Console.WriteLine("Table fields:");
foreach (DataColumn col in table.Columns)
Console.WriteLine(col.ColumnName);
Console.WriteLine(" ");
Console.WriteLine("Rows: " + table.Rows.Count);
Console.Read();
}
If Advantage data providers were installed correctly and there are no errors in table names, locations and your SQL query then you should see list of table column names and row count on console window when you run the application.
View Comments (2)
I am trying to open a .adt extension table from .Net,
i am able to read all the free tables, but these table with password cannot open and fetch the data.
Anybody please help me how to use my password('XYZABD') to fetch the table data to my code.
Query for the Freetables i wrote:
"Select * from USERS";
How to write query for particular password protected free table...?
Do you still have the latest installer for the provider? Sap Website Download Need a S ID which you need to pay just for a Provider not the server.
Not like MSSQL where you can download for free.