[英]Add MS Access table data to SQL Server table with c#
I have a MS Access file with a Number
table. 我有一个带
Number
表的MS Access文件。 Also I have a SQL Server database with a profile
table. 我也有一个带有
profile
表的SQL Server数据库。
How do I import data from the number
table into the profile
table? 如何将
number
表中的数据导入profile
表?
It will be something like this: 将会是这样的:
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.