我有一个带有Number
表的MS Access文件。我还有一个带有profile
表的SQL Server数据库。
如何将number
表中的数据导入profile
表?
答案 0 :(得分:3)
会是这样的:
const string connectionString = "YOUR ACCESS CONNECTION STRING";
const string connectionStringDest = "YOUR SQL SERVER CONNECTION STRING";
using (var sourceConnection =
new OleDbConnection(connectionString))
{
sourceConnection.Open();
var commandSourceData = new OleDbCommand(
"SELECT COL1, COL2 FROM TABLE_X;", sourceConnection);
var reader =
commandSourceData.ExecuteReader();
using (var destinationConnection =
new SqlConnection(connectionStringDest))
{
destinationConnection.Open();
using (var bulkCopy =
new SqlBulkCopy(destinationConnection))
{
bulkCopy.DestinationTableName =
"dbo.TABLE_DEST";
try
{
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
reader.Close();
}
}
}
}