sqlbulkcopy使用sql CE

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

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

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

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行

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

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

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

  1. 确保sorce和目标表具有相同的字段结构;
  2. 使用源数据库中的数据表克隆虚拟数据表(您的选择);
  3. 创建一个CE命令,其表名为commandtext(TableDirect as commandtype);
  4. 从CE命令创建CE dataadapter;
  5. 从CE dataatapter创建CE命令构建器;
  6. 将CE命令构建器中的Insert命令传递给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 

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

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

当然,一切都取决于你的桌子的结构。 IMAGE和NTEXT都是慢数据类型。

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

不,我不认为支持SqlBulkCopy (参见MSDN )。 也许将数据作为xml抛出并在服务器上将其拆开? SQL / XML在2005/2008年非常好。

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