我在 Access 中有一个包含 A、B 和 C 列的表。我想将此表中的记录写入包含 A、B、C 和 D 列的 SQL 表中。实现它的最佳方法是什么?
我尝试使用 OleDbDataReader 和 bulkCopy,但架构差异使其变得困难:
OleDbDataReader reader;// = new OleDbDataReader();
using (OleDbConnection sourceConnection = new OleDbConnection(sourceConnectionString))
{
//Get data from Access
OleDbCommand commandSourceData = new OleDbCommand("SELECT * FROM AddressLoCo;", sourceConnection);
commandSourceData.Connection = sourceConnection;
sourceConnection.Open();
reader = commandSourceData.ExecuteReader();
using (SqlConnection destinationConection = new SqlConnection(destinationConnectionString))
{
destinationConection.Open();
// Set up the bulk copy object
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConection))
{
bulkCopy.DestinationTableName = "dbo.AddressMaster";
try
{
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
reader.Close();
}
}
}
}