I have a DBF file which I am trying to import and then write that to a SQL table. The issue I am running into is if I use SqlBulkCopy, it requires me to create the table in advance but that is not possible in my scenario as the dbf file changes constantly.
Here is my code so far:
public void saveDBF()
{
//define the connections to the .dbf file
OleDbConnection oConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Path.GetDirectoryName(tbFile.Text)+";Extended Properties=dBase III");
OleDbCommand command = new OleDbCommand("select * from " + Path.GetFileName(tbFile.Text), oConn);
//open the connection and read in all the airport data from .dbf file into a datatable
oConn.Open();
DataTable dt = new DataTable();
dt.Load(command.ExecuteReader());
oConn.Close(); //close connection to the .dbf file
//create a reader for the datatable
DataTableReader reader = dt.CreateDataReader();
myConnection = new SqlConnection(cString);
myConnection.Open(); ///this is my connection to the sql server
SqlBulkCopy sqlcpy = new SqlBulkCopy(myConnection);
sqlcpy.DestinationTableName = "TestDBF"; //copy the datatable to the sql table
sqlcpy.WriteToServer(dt);
myConnection.Close();
reader.Close();
}
It keeps failing at sqlcpy.WriteToServer(dt);
stating it cannot access the destination table.
Is there an option in C# to create the table on the fly before writing to that table?