C # open dbf file

I had a problem opening a DBF file - I need to open it, read everything and process it. I tried several solutions (ODBC / OLEDB), several connection strings, but so far nothing has worked.

The problem is that when I execute the SQL command to get everything from the file, nothing is returned - there are no rows. Even stranger, the contents of the opened DBF file are deleted.

See the code I have:

public override bool OpenFile(string fileName, string subFileName = "") { OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(fileName) + ";Extended Properties=dBASE IV;User ID=;Password=;"); try { if (con.State == ConnectionState.Closed) { con.Open(); } OleDbDataAdapter da = new OleDbDataAdapter("select * from " + Path.GetFileName(fileName), con); DataSet ds = new DataSet(); da.Fill(ds); con.Close(); int i = ds.Tables[0].Rows.Count; return true; } catch { return false; } } 

I debugged the code and watched the file open in Windows Explorer. When it reaches this line:

 da.Fill(ds); 

file size dropped to a few bytes (from hundreds of kB).

My next thought was to make the DBF file read-only. This nonetheless raises an "unexpected exception from the external driver."

So my question is what the hell? I am sure that the file is not corrupted, this is a direct export from some kind of database. (No, I do not have access to this database). I can also open this file in MS Office without problems.

I can not share the DBF file - it contains sensitive data.

+6
source share
2 answers

Two things ... just because the .DBF file extension can mean that it is a Dbase IV file. Actually it could be Visual Foxpro. However, I would look at downloading and installing the Visual FoxPro OleDB driver from a Microsoft download . Then OleDbConnection points to a path that has the actual tables (you already have).

The request itself should not care about the extension, so I would change your call to get the name through "Path.GetFileNameWithoutExtension" then

It could be a combination of the two.

VFP Provider Connection String

 "Provider=VFPOLEDB.1;Data Source=" + FullPathToDatabase 
+11
source

This is not an exact answer, but it will help you find the problem.

Try specifying an inline connection string and select a query to verify that the problem is not related to their creation. Catch the exception and check its details.

 OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\folder;Extended Properties=dBASE IV;User ID=;Password=;"); // give your path directly try { con.Open(); OleDbDataAdapter da = new OleDbDataAdapter("select * from tblCustomers.DBF", con); // update this query with your table name DataSet ds = new DataSet(); da.Fill(ds); con.Close(); int i = ds.Tables[0].Rows.Count; return true; } catch(Exception e) { var error = e.ToString(); // check error details return false; } 
+2
source

Source: https://habr.com/ru/post/900712/


All Articles