.NET 连接到 dBase .dbf 文件

发布于 2024-08-17 20:20:45 字数 2072 浏览 5 评论 0原文

我正在尝试使用 .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();
        }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌味 2024-08-24 20:20:45

试试这个:

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

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

失与倦" 2024-08-24 20:20:45

简单来说它无法工作,因为 64 位系统不再支持 64 位的 odbc dbf 驱动程序,jet 也不再存在。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文