title: C# Open DBF file
body: <p>I'm having a problem opening a DBF file - I need to open it, read everything and process it. I tried several solutions (ODBC/OLEDB), several connection string, but nothing worked so far. </p>
  
  <p>The problem is, when I execute the SQL command to get everything from the file, nothing gets returned - no rows. What's even more odd, the content of the DBF file being opened get deleted.</p>
  
  <p>See the code I have:</p>
  
  <pre><code>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;
      }             
  }
  </code></pre>
  
  <p>I debugged the code and watched the file being opend in Windows Explorer. When it reached this line:</p>
  
  <pre><code>da.Fill(ds);
  </code></pre>
  
  <p>the size of  the  file dropped to only a few Bytes (from hundreds of kB).</p>
  
  <p>My next thought was to make the DBF file read only. That however cause an "unexpected exception from an external driver".</p>
  
  <p>So my question is - what the heck? I'm sure the file is <em>not</em> corrupt, it is a direct export from some DB. (No, I do not have access to that DB). I can also open that file in MS Office no problem.</p>
  
  <p><sup>I cannot share the DBF file - it contains confidential data.</sup></p>