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



我正在尝试使用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.__ComObjectIRowset。当我尝试使用OleDbAdapter填充表时,一切都很好。
我如何使用IDataReader读取。dbf文件?

我认为你的路径可能是错误的,因为你在connectionstring中有"C:mybase",然后添加"我的。

<C:mybase>

我将提供如何使用数据集而不是阅读器打开和读取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
        }

好的,尝试使用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

最新更新