Home > database >  How can I read a dbase4 encoded file in the easiest way with C#? I have tried with OleDB but got a l
How can I read a dbase4 encoded file in the easiest way with C#? I have tried with OleDB but got a l

Time:10-02

I am reading machine datas from a Siemens CNC control system 840D powerline. I can download the machine data but as it is encoded with dbase4 I cannot read the file. Only the ASCII characters can be read, but there are some other charachters between them. I have googled a littele bit and found a solution with OleDB. But I couldn't get it.

public void dbase()
{
    try
    {
        string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\test;Extended Properties=dBASE IV;User ID=Admin;Password=;";
        using (OleDbConnection con = new OleDbConnection(constr))
        {
            var sql = "select * from "   "NC_MD_ACC";
            OleDbCommand cmd = new OleDbCommand(sql, con);
            con.Open();
            DataSet ds = new DataSet(); ;
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(ds);
            Deltalogic.NC_VAR_test_string = ds.Tables[0].ToString();
            using (StreamWriter sw = File.AppendText(CommonClass.error_path))
            {
                sw.WriteLine("OdebD OK  "   Convert.ToString(DateTime.Now));
            }
        }
    }

    catch (Exception e)
    {
        using (StreamWriter sw = File.AppendText(CommonClass.error_path))
        {
            sw.WriteLine("OdebD Error  "   e   Convert.ToString(DateTime.Now));
        }

    }
}

I got this Error:

OdebD Error  System.TypeInitializationException: The type initializer for   'System.Data.OleDb.OleDbConnection' threw an exception.
---> System.TypeInitializationException: The type initializer for   'System.Data.OleDb.OleDbConnectionFactory' threw an exception.
---> System.TypeInitializationException: The type initializer for 'System.Data.ProviderBase.DbConnectionPoolCountersNoCounters' threw an exception.
---> System.TypeInitializationException: The type initializer for 'CreationData' threw an   exception.
---> System.PlatformNotSupportedException: Performance Counters are not supported on this platform.
at System.Diagnostics.CounterCreationData..ctor(String counterName, String counterHelp, PerformanceCounterType counterType)
at System.Data.ProviderBase.DbConnectionPoolCounters.CreationData..cctor()
--- End of inner exception stack trace ---
at System.Data.ProviderBase.DbConnectionPoolCounters..ctor(String categoryName, String categoryHelp)
at System.Data.ProviderBase.DbConnectionPoolCounters..ctor()
at System.Data.ProviderBase.DbConnectionPoolCountersNoCounters..ctor()
at System.Data.ProviderBase.DbConnectionPoolCountersNoCounters..cctor()
--- End of inner exception stack trace ---
at System.Data.ProviderBase.DbConnectionFactory..ctor()
at System.Data.OleDb.OleDbConnectionFactory..ctor()
at System.Data.OleDb.OleDbConnectionFactory..cctor()
  --- End of inner exception stack trace ---
at System.Data.OleDb.OleDbConnection..cctor()
--- End of inner exception stack trace ---
at System.Data.OleDb.OleDbConnection.get_ConnectionFactory()
at System.Data.OleDb.OleDbConnection.ConnectionString_Set(DbConnectionPoolKey key)
at System.Data.OleDb.OleDbConnection.ConnectionString_Set(String value)
at System.Data.OleDb.OleDbConnection.set_ConnectionString(String value)
at System.Data.OleDb.OleDbConnection..ctor(String connectionString)
at WebApplication7.Pages.PLC_Var.dbase() in C:\01_md9bu_Projects\ASP\WebApplication7\Pages\PLC_Var.razor.cs:line 37010/1/2022 1:57:22 PM

CodePudding user response:

If your intention is just reading and you don't want to use/install oledb packages(and you don't want to depend on windows), you can use https://github.com/henck/dBASE.NET

It is easy to use:

using dBASE.NET;

var dbf = new Dbf();
dbf.Read("database.dbf");

foreach(var field in dbf.Fields) {
    Console.WriteLine(field.Name);
}


foreach(var record in dbf.Records) {
    for(int i = 0;  i < dbf.Fields.Count; i  ) {
        Console.WriteLine(record[i]);
    }
}
  • Related