4

我的代码中有一个填充的 DataTable:

我正在使用 SQL Server CE 4.0 并解决性能问题,我正在使用SqlCeBulkCopy

SqlCeBulkCopyOptions options = new SqlCeBulkCopyOptions();
options = options |= SqlCeBulkCopyOptions.KeepNulls;

// Check for DB duplicates
using (SqlCeBulkCopy bc = new SqlCeBulkCopy(strConn, options))
{
    dt = RemoveDuplicateRows(dt, "Email");
    bc.DestinationTableName = "Recipients";
    bc.WriteToServer(dt);
}

RemoveDuplicateRows将从 DataTable 中删除重复项,但不会检查数据库中已存在的内容。

在将 DataTable 传递给WriteToServer(dt).

什么是解决这个问题的良好性能、成本效益的解决方案?

4

1 回答 1

1

所以你需要对数据表和现有表进行标记吗?我不确定sql ce是否支持临时表,我用ms sql做了一些类似的事情,这里是伪代码

string tmpTableDefinition = "create table #tmpEmails (...)";
using(var connection = new SqlCeConnection(connectionString))
{
    //Create temp table
    var tmpTableCommand = new SqlCeCommand(tmpTableDefiniton, connection);
    tmpTableCommand.ExecuteNonQuery();
    //Bulk copy to the temp table, note that bulk copy run faster if the teble is empty
    //which is always true in this case...
    using (var bc = new SqlCeBulkCopy(connection, options))
    {
         bc.DestinationTableName = "#tmpEmails";
         bc.WriteToServer(dt);
    }
    //Run a sp, that have temp table and original one, and marge as you wish in sql
    //for sp to compile properly, you would have to copy tmp table to script too
    var spCommand = new SqlCommand("sp_MargeTempEmailsWithOriginal", connection);
    spCommand.Type = SP //Don't remember exact prop name and enum value
    spCommand.ExecuteNonQuery();
}
于 2013-08-15T09:51:41.730 回答