[英]Add MS Access table data to SQL Server table with c#
我有一個帶Number
表的MS Access文件。 我也有一個帶有profile
表的SQL Server數據庫。
如何將number
表中的數據導入profile
表?
將會是這樣的:
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();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.