我有一个DBF文件,正在尝试将其导入并将其写入SQL表中。我遇到的问题是,如果使用SqlBulkCopy,它要求我提前创建表,但在我的情况下不可能,因为dbf文件会不断变化。
这是我到目前为止的代码:
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();
}
在sqlcpy.WriteToServer(dt);
时它一直失败,并声称无法访问目标表。
C#中是否有选项可以在写入该表之前动态创建该表?