title: How can I save a DataTable to a .DBF?
body: <p>I've been working on a program to read a dbf file, mess around with the data, and save it back to dbf. The problem that I am having is specifically to do with the writing portion.</p>
  
  <pre><code>    private const string constring = "Driver={Microsoft dBASE Driver (*.dbf)};"
                                     + "SourceType=DBF;"
                                     + "DriverID=277;"
                                     + "Data Source=¿;"
                                     + "Extended Properties=dBASE IV;";
      private const string qrystring = "SELECT * FROM [¿]";
      public static DataTable loadDBF(string location)
      {
          string filename = ConvertLongPathToShort(Path.GetFileName(location));
          DataTable table = new DataTable();
          using(OdbcConnection conn = new OdbcConnection(RTN(constring, filename)))
          {
              conn.Open();
              table.Load(new OdbcCommand(RTN(qrystring, filename), conn).ExecuteReader());
              conn.Close();
          }
          return table;
      }
  
      private static string RTN(string stmt, string tablename)
      { return stmt.Replace("¿", tablename); }
  
      [DllImport("Kernel32", CharSet = CharSet.Auto)]
      static extern Int32 GetShortPathName(
      String path,                // input string
      StringBuilder shortPath,    // output string
      Int32 shortPathLength);     // StringBuilder.Capacity
  
      public static string ConvertLongPathToShort(string longPathName)
      {
          StringBuilder shortNameBuffer;
          int size;
  
          shortNameBuffer = new StringBuilder();
  
          size = GetShortPathName(longPathName, shortNameBuffer, shortNameBuffer.Capacity);
          if (size &gt;= shortNameBuffer.Capacity)
          {
              shortNameBuffer.Capacity = size + 1;
              GetShortPathName(longPathName, shortNameBuffer, shortNameBuffer.Capacity);
          }
  
          return shortNameBuffer.ToString();
      }
  </code></pre>
  
  <p>This is what I'm working with. I've tried a number of methods to write a new file, none of them productive. To be honest, while normally I would be an advocate of form and function, I just want the damn thing to work, this app is supposed to do one very specific thing, it's not going to simulate weather.</p>
  
  <p>-=# Edit #=-</p>
  
  <p>I've since discontinued the app due to time pressure, but before I scrapped it I realised that the particular format of dbf I was working with had no primary key information. This of course meant that I had to essentially read the data out to DataTable, mess with it, then wipe all the records in the dbf and insert everything from scratch.
  Screw that for a lark.</p>