15

是否可以将 SqlBulkcopy 与 Sql Compact Edition 一起使用,例如 (*.sdf) 文件?

我知道它适用于 SQL Server 200 Up,但想检查 CE 兼容性。

如果没有其他人知道在不使用 DataSets 的情况下将 CSV 类型文件导入 SQL Server CE 的最快方法(这里吐槽)?

4

4 回答 4

24

SQL CE 不支持 BULKCOPY。如果表中有大量行,这是最快的方法;插入太慢了!

using (SqlCeConnection cn = new SqlCeConnection(yourConnectionString))
{
    if (cn.State == ConnectionState.Closed)
        cn.Open();

    using (SqlCeCommand cmd = new SqlCeCommand())
    {
        cmd.Connection = cn;
        cmd.CommandText = "YourTableName";
        cmd.CommandType = CommandType.TableDirect;

        using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable | ResultSetOptions.Scrollable))
        {
            SqlCeUpdatableRecord record = rs.CreateRecord();

            using (var sr = new System.IO.StreamReader(yourTextFilePath))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    int index = 0;
                    string[] values = line.Split('\t');

                    //write these lines as many times as the number of columns in the table...
                    record.SetValue(index, values[index++] == "NULL" ? null : values[index - 1]);
                    record.SetValue(index, values[index++] == "NULL" ? null : values[index - 1]);
                    record.SetValue(index, values[index++] == "NULL" ? null : values[index - 1]);

                    rs.Insert(record);
                }
            }
        }
    }
}

基准:34370 行的表

  • 插入:每秒写入 38 行

  • 这样:每秒写入 260 行

于 2009-10-23T09:57:14.010 回答
9

我在这里有一个 SqlCeBulkCopy 库:http://sqlcebulkcopy.codeplex.com -甚至支持 IEnumerable。

于 2011-04-20T16:19:30.870 回答
1

有可能增加很多这种操作。要使此操作有用(我的意思是快速且非常安全),您可以使用 CE DataAdapter。

通过示例,不关心密钥,下面列出的步骤可以帮助您:

  1. 确保源表和目标表具有相同的字段结构;
  2. 使用源数据库中的数据表克隆虚拟数据表(您的选择);
  3. 创建一个CE命令,表名为commandtext(TableDirect为commandtype);
  4. 从 CE 命令创建一个 CE 数据适配器;
  5. 从 CE dataapter 创建一个 CE commandbuilder;
  6. 将插入命令从 CE commandbuilder 传递到 CE dataadapter;
  7. 将“n”批行从源数据表复制到目标数据表(克隆),执行如下操作:

    '... previous codes
    For Each currentRow In sourceTable.Rows
       'u can do RaiseEvent Processing(currentRow, totalRows) here with DoEvents
       If targetTable.Rows.Count < 100 Then
          targetTable.InportRow(currentRow)
          targetTable.Rows(targetTable.Rows.Count - 1).SetAdded
       Else
          '...Here you wll call the CE DataAdapter's Update method (da.Update(targetTable))
          '...and then be sure you clone the targetTable again, erasing all previous  rows.
          '...Do a clone again, don't do just a "clear" in the Rows collection.  
          '...If u have an Autoincrement it will break all Foreign Keys.
       End If
       Next
       '... next codes
    

通过这种方式,您可以在没有太多时间的情况下更新几行。

我有一些使用这种方法的应用程序,在具有 5 个 NTEXT 字段(慢)和 800000 行的表中,平均速率约为每秒 1500 行。

当然,这一切都取决于您的表的结构。IMAGE 和 NTEXT 都是慢速数据类型。

PS:正如我所说,这种方法不太关心密钥,所以要小心。

于 2010-09-05T18:53:00.670 回答
0

不,我认为不SqlBulkCopy支持(请参阅MSDN)。也许将数据作为 xml 放入并在服务器上将其剥离?SQL/XML 在 2005/2008 年相当不错。

您可能还想查看 table-value-parameters,但我怀疑 CE 是否支持这些。

于 2009-10-22T11:09:08.520 回答