.NET连接到dBase .dbf文件

时间:2010-01-14 16:45:32

标签: .net winforms dbase

我正在尝试使用.NET和Winforms读取dBase III .dbf文件,我尝试过的任何内容似乎都无法正常工作。我尝试了四种不同的连接方法,每种方法都挂在Open方法上。没有例外,没有超时,没有事件消息,没有。表格就在那里。关于什么可能是错的任何想法?

这是我尝试过的方法。 .dbf文件位于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();
        }

2 个答案:

答案 0 :(得分:4)

试试这个:

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]);
        }
    }
}

我刚刚运行这个没有错误。此网站非常有用:ConnectionStrings.com

答案 1 :(得分:1)

简单地说它无法工作,因为64位系统不再支持64位的喷射dbf驱动程序,也不再存在喷射...