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.

3
  • What is the data type of those columns, perhaps you should call GetString msdn.microsoft.com/en-us/library/… instead of retrieving the native format. Commented May 8, 2012 at 9: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 Commented May 8, 2012 at 9: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. Commented May 8, 2012 at 9:33

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.