ODBC does not support reading DBF file?

Asked

Viewed 631 times

3

I’m trying to read a file .dbf following a tutorial I saw on the site of Macoratti (Obs. I think everyone knows). And when executing the method below, is launched a OdbcException with the following message: ERROR [IM001] [Microsoft][ODBC Driver Manager] O driver não oferece suporte para esta função

        private DataTable lerDbf(string filename)
        {
            using (OdbcConnection conn = new OdbcConnection("Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" +
                                         System.IO.Path.GetFullPath(filename).Replace(System.IO.Path.GetFileName(filename), "") + ";Exclusive=No"))
            {
                string consulta = "SELECT * FROM [" + System.IO.Path.GetFileName(filename) + "]";
                OdbcDataAdapter adapter = new OdbcDataAdapter(consulta, conn);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds.Tables[0];
            }
        }

Regarding the tutorial and the files, on the website it says:

[...] just select the default Dbase Foxpro file and import...

The archives .dbf which I have to read in the system is generated from a .xls. Is this the problem? What happens? There is something in the code, maybe in the connection string that is wrong?

  • You could link the tutorial. I saw another one on the same site that doesn’t speak in Foxpro, and has examples for ODBC and Oledb. http://www.macoratti.net/08/net_dbf1.htm. I don’t know the details of these drivers, but it might actually be that the driver can’t do all the operations that ADO.Net requires. Then you would have to limit what to use. If you have many complications, it would be interesting to evaluate the possibility of importing data from dbf, even if you have to do this several times a day in an automated way. Another possibility is to use the xls direct and forget the dbf.

  • I don’t understand what you’re doing here: Replace(System.IO.Path.GetFileName(filename), "")

  • @bigown here is the link http://www.macoratti.net/12/04/c_dbf1.htm

  • Try doing the one I gave you and see what happens. Here’s a reference to Connection strings if it helps: http://www.connectionstrings.com/dbf-foxpro/

  • I had found this site once, very good. Ref. to the link you passed, you suggest I do with oledb or odbc?

  • @bigown had found an OS link and I ended up following him before checking the link you gave me, it worked perfectly! http://stackoverflow.com/questions/11356878/get-data-in-a-dbf-file-using-c-sharp

  • I have a different opinion, because many things there have problems, but it has several things :) I always prefer Oledb before ODBC, but use what works. I never had to access the dbf directly, I only imported, so I can only give ideas.

  • 1

    I was looking in the OR now. Congratulations. Put as an answer and accept it then.

Show 3 more comments

1 answer

2


In this case the problem was really in the connection string. In this OS question - https://stackoverflow.com/questions/11356878/get-data-in-a-dbf-file-using-c-sharp - the correct form is found.

string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourfilepath;Extended Properties=dBASE IV;User ID=Admin;Password=;";
using (OleDbConnection con = new OleDbConnection(constr))
            {
                var sql = "select * from " + fileName;
                OleDbCommand cmd = new OleDbCommand(sql, con);
                con.Open();
                DataSet ds = new DataSet(); ;
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                da.Fill(ds);
            }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.