在C#中使用IDataReader读取dbf文件

发布于 2024-11-19 07:16:27 字数 699 浏览 2 评论 0原文

我正在尝试使用 OleDb 使用数据读取器读取 .dbf 文件,如下所示:

const string OleDbConnectionString =
    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydbase;Extended Properties=dBASE IV;";
    var connection = new OleDbConnection(OleDbConnectionString);
    connection.Open();

    var command = new OleDbCommand("select * from my.dbf", connection);

    reader = command.ExecuteReader();
    Console.WriteLine(reader.Read()); // true
    Console.WriteLine(reader[0].ToString()); // exception

异常是 InvalidCastException 类型,并表示:无法从 System.__ComObject 转换为 IRowset。 当我尝试使用 OleDbAdapter 填充表格时,一切正常。
如何使用 IDataReader 读取 .dbf 文件?

I'm trying to read .dbf file with datareader using OleDb like this:

const string OleDbConnectionString =
    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydbase;Extended Properties=dBASE IV;";
    var connection = new OleDbConnection(OleDbConnectionString);
    connection.Open();

    var command = new OleDbCommand("select * from my.dbf", connection);

    reader = command.ExecuteReader();
    Console.WriteLine(reader.Read()); // true
    Console.WriteLine(reader[0].ToString()); // exception

The exception is of InvalidCastException type and says: Unable to case from System.__ComObject to IRowset.
When I tried to use OleDbAdapter to fill a table everything worked fine.
How do I read .dbf file using IDataReader?

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

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

发布评论

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

评论(2

荒岛晴空 2024-11-26 07:16:27

我认为您的路径可能是错误的,因为您在连接字符串中有“C:\mybase”,然后添加“my.dbf”,加起来为“C:\mybasemy.dbf”。

我将提供如何使用数据集而不是阅读器打开和读取 dbf 的代码。

string oledbConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\spcs\;Extended Properties=dBASE IV;User ID=Admin;Password=";

        OleDbConnection oledbConnection = new OleDbConnection(oledbConnectionString);

        string oledbQuery = @"SELECT * FROM KUND";

        try
        {
            OleDbCommand oledbCommand = new OleDbCommand(oledbQuery, oledbConnection);

            OleDbDataAdapter oledbAdapter = new OleDbDataAdapter(oledbCommand);

            DataSet oledbDataset = new DataSet();

            oledbAdapter.FillSchema(oledbDataset, SchemaType.Mapped);

            oledbConnection.Open();

            oledbAdapter.Fill(oledbDataset);

            foreach (DataRow row in oledbDataset.Tables[0].Rows)
            {
                System.Diagnostics.Trace.WriteLine(row[0].ToString());
            }
        }
        catch (Exception ex)
        {
            // Do something with ex
        }

I think your path might be wrong since you have "C:\mybase" in the connectionstring and then add "my.dbf" which adds up to "C:\mybasemy.dbf".

Ill provide code how i open and read a dbf using a dataset instead of a reader.

string oledbConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\spcs\;Extended Properties=dBASE IV;User ID=Admin;Password=";

        OleDbConnection oledbConnection = new OleDbConnection(oledbConnectionString);

        string oledbQuery = @"SELECT * FROM KUND";

        try
        {
            OleDbCommand oledbCommand = new OleDbCommand(oledbQuery, oledbConnection);

            OleDbDataAdapter oledbAdapter = new OleDbDataAdapter(oledbCommand);

            DataSet oledbDataset = new DataSet();

            oledbAdapter.FillSchema(oledbDataset, SchemaType.Mapped);

            oledbConnection.Open();

            oledbAdapter.Fill(oledbDataset);

            foreach (DataRow row in oledbDataset.Tables[0].Rows)
            {
                System.Diagnostics.Trace.WriteLine(row[0].ToString());
            }
        }
        catch (Exception ex)
        {
            // Do something with ex
        }
彩扇题诗 2024-11-26 07:16:27

好的,尝试使用 GetString:

const string OleDbConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydbase;Extended Properties=dBASE IV;";
OleDbConnection connection = new OleDbConnection(OleDbConnectionString);
connection.Open();

OleDbCommand command = new OleDbCommand("select * from my.dbf", connection);

OleDbDataReader reader = command.ExecuteReader();
Console.WriteLine(reader.Read()); // true
Console.WriteLine("{0}", reader.GetString(0)); // exception

Okay, try using GetString:

const string OleDbConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydbase;Extended Properties=dBASE IV;";
OleDbConnection connection = new OleDbConnection(OleDbConnectionString);
connection.Open();

OleDbCommand command = new OleDbCommand("select * from my.dbf", connection);

OleDbDataReader reader = command.ExecuteReader();
Console.WriteLine(reader.Read()); // true
Console.WriteLine("{0}", reader.GetString(0)); // exception
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文