有SQL Server数据库表,它是源表,而Postgres数据库是目标表。是否有任何有效的方法使用C#将相同的数据保留在反映SQL Server表的postgres表一侧?
到目前为止,我一直尝试同时使用System.Data
和Npgsql
。我的想法是每次删除目标表中的数据,然后将所有数据从源表中批量删除。 SqlBulkCopy
很不错,但是来自System.Data
,我在Npgsql
中看不到。有人可以帮我吗?
private static void PerformBulkCopy()
{
string SourcedestconnectionString = @"";
string TarettargconnectionString = @"";
// get the source data
using (SqlConnection sourceConnection = new SqlConnection(SourcedestconnectionString))
{
SqlCommand myCommand = new SqlCommand("SELECT * FROM [pub].[vw_Services_NDPEWork]", sourceConnection);
sourceConnection.Open();
SqlDataReader reader = myCommand.ExecuteReader();
// open the destination data
using (NpgsqlConnection destinationConnection = new NpgsqlConnection(TarettargconnectionString))
{
// open the connection
destinationConnection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection.ConnectionString))
{
bulkCopy.BatchSize = 500;
bulkCopy.NotifyAfter = 1000;
bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(Target);
bulkCopy.DestinationTableName = "roberttest";
bulkCopy.WriteToServer(reader);
}
}
reader.Close();
}
}