.NET Connection to dBase .dbf file
Asked Answered
F

2

1

I'm trying to read a dBase III .dbf file using .NET and Winforms and nothing I've tried seem to work. I tried four different connection methods and every one of them hangs on Open method. No exceptions, no timeouts, no event messages, nothing. The form just sits there. Any ideas about what could be wrong?

Here's the methods I've tried. The .dbf file is at d:\db:

private void read1()
        {
            string c = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\db\\;Extended Properties=dBASE III";
            OleDbConnection conn = new OleDbConnection(c);
            conn.Open();
            MessageBox.Show("ok");
            conn.Close();

        }

        private void read2()
        {
            System.Data.Odbc.OdbcConnection oConn = new System.Data.Odbc.OdbcConnection();
            oConn.ConnectionString = @"Driver={Microsoft dBase Driver (*.dbf)};SourceType=DBF;SourceDB=D:\db;Exclusive=No; Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;";
            oConn.Open();
            System.Data.Odbc.OdbcCommand oCmd = oConn.CreateCommand();
            oCmd.CommandText = @"SELECT * FROM D:\db\Poi.dbf";
            DataTable dt = new DataTable();
            dt.Load(oCmd.ExecuteReader());
            MessageBox.Show(dt.Rows.Count.ToString());
            oConn.Close();
        }

        private void read3()
        {
            System.Data.Odbc.OdbcConnection oConn = new System.Data.Odbc.OdbcConnection();
            oConn.ConnectionString = @"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=d:\db;";
            oConn.Open();
            MessageBox.Show("ok");
            oConn.Close();
        }

        private void read4()
        {
            System.Data.Odbc.OdbcConnection oConn = new System.Data.Odbc.OdbcConnection();
            oConn.ConnectionString = @"Driver={Microsoft dBase Driver (*.dbf)};datasource=d:\db\";
            oConn.Open();
            System.Data.Odbc.OdbcCommand oCmd = oConn.CreateCommand();
            oCmd.CommandText = @"SELECT * FROM D:\db\Poi.dbf";
            DataTable dt = new DataTable();
            dt.Load(oCmd.ExecuteReader());
            MessageBox.Show(dt.Rows.Count.ToString());
            oConn.Close();
        }
Fascism answered 14/1, 2010 at 16:45 Comment(1)
I wanna to help, but where I find a DBF file this days? =)Tabber
T
4

Try this:

using (OleDbConnection cn = new OleDbConnection(
    @"Provider=Microsoft.Jet.OLEDB.4.0;" + 
    @"Data Source=C:\full-path-without-filename;" + 
    @"Extended Properties=dBASE III;"))
using (OleDbCommand cm = cn.CreateCommand())
{
    cn.Open();
    cm.CommandText = "SELECT * FROM fox_samp"; // fox_samp is your filename
    using (OleDbDataReader dr = cm.ExecuteReader())
    {
        while (dr.Read())
        {
            Console.WriteLine(dr[0]);
        }
    }
}

I just ran this without errors. And this site can be very useful: ConnectionStrings.com

Tabber answered 14/1, 2010 at 17:7 Comment(2)
Thanks but this didn't work either. It hangs on Open(). I think something is wrong with the file or my computer..Fascism
I suggest you to build a console application and to make a very simple example, like above; or you could also to use OdbcConnection and related classesTabber
M
1

Simply it cannot work since 64 bit systems no more support odbc dbf driver for 64 bit nor jet exists anymore..

Mattheus answered 10/6, 2014 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.