0

I've got a c# program, which is connected to dbf file:

OdbcConnection oconn = new OdbcConnection();
oconn.ConnectionString =
    "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + pelna_sciezka + ";Exclusive=No; Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;";
oconn.Open();
OdbcCommand ocmd = oconn.CreateCommand();

ocmd.CommandText = @"SELECT * FROM " + pelna_sciezka + " where Kod_kontr = '" + row.KNH_KOD + "'";
// ocmd.ExecuteNonQuery();
OdbcDataReader odr = ocmd.ExecuteReader();
while (odr.Read())
{
    kod_kontr = odr["Kod_kontr"].ToString();
    Nzwakontr1 = odr["Nzwakontr1"];
    Nzwakontr2 = odr["Nzwakontr2"];
}

connection is working very well, but when I want to assemble data to local string variables (kod_kontr, nzwakontr1), all i get for a value is System.Byte[]. When I want to get the other types of data (f.ex. date, numeric, etc)from this dbf, everything is working well. The problem is only for varchar data . How could I solve my problem?

Thanx for any help


According to Antonio Bakula help i've read the answer:

I must change from ODBC to OLE, and: - change connectionstring to: oconn.ConnectionString = "Provider=vfpoledb.1;Data Source="+pelna_sciezka+";Collating Sequence=machine"; change code for that:

OleDbDataReader odr = ocmd.ExecuteReader();

            while (odr.Read())
            {
              //  byte[] A = Encoding.GetEncoding(Encoding.Default.CodePage).GetBytes(odr.GetString(0));
              //  string p = Encoding.Unicode.GetString((Encoding.Convert(Encoding.GetEncoding(850), Encoding.Unicode, A)));

                kod_kontr = OdczytajTabliceBajtow(odr["Kod_kontr"]);
                Nzwakontr1 = OdczytajTabliceBajtow(odr["Nzwakontr1"]);
                Nzwakontr2 = OdczytajTabliceBajtow(odr["Nzwakontr2"]);
            }

where OdczytajTabliceBajtow:

private string OdczytajTabliceBajtow(object p) { Encoding enc8 = Encoding.ASCII; string wynik = ""; Byte[] bytes = (Byte[])p; StringBuilder sb = new StringBuilder(); sb.Append(Encoding.ASCII.GetChars(bytes)); wynik = sb.ToString(); return wynik; }

This is the solution of my problem. Thank you all for help.

BKl
  • 415
  • 1
  • 8
  • 23
  • What is the data type of those columns, perhaps you should call `GetString` http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcdatareader.getstring.aspx instead of retrieving the native format. – Jodrell May 08 '12 at 09:06
  • this columns have a varchar type ( C(100) in dbf structure ). Other types is converting very well, only with 'C' type get a problem – BKl May 08 '12 at 09:11
  • As the documentation states, the index accesor "Gets the value of the specified column in its native format", it just so happens that the native format of some of the columns are compatible with your desired type. It seems the index accessor returns VFP text data as a byte array, not entirely unreasonable. If you know the encoding of the text you can convert to a .Net string using the `System.Text` namespace. If you don't, you could call `GetString` and let the ODBC layer do the conversion for you. – Jodrell May 08 '12 at 09:33

1 Answers1

3

I reccomend that you use OleDB driver for FoxPro and you will not have these problems, and there will be noticable gain in speed, here is the link

http://www.microsoft.com/en-us/download/details.aspx?id=14839

And then you will get value from DataReader as string for Character fields

Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102