2009-10-22 34 views
14

是否可以在Sql Compact Edition中使用SqlBulkcopy (* .sdf)文件?使用sql的sqlbulkcopy CE

我知道它适用于SQL Server 200以上版本,但希望检查CE兼容性。

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

回答

0

不,我认为SqlBulkCopy不受支持(请参阅MSDN)。也许把数据放在xml中,然后在服务器上拆开它? SQL/XML在2005/2008非常好。

您可能也想看一下表值参数,但我怀疑CE是否支持这些参数。

22

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行每秒

+0

有趣的是,谢谢:) – leppie 2009-10-23 09:59:58

+0

这种方法不支持标识和rowversion列类型 – 2011-09-13 04:57:36

+2

哇,一直在努力与插入和获得20-30行每秒,我敢肯定,这只是做了所有35,774行不到1第二(我所有的数据都在内存中的一个数组中),所以没有来自源头的瓶颈。 – Matt 2012-02-01 16:13:35

1

写入是可能增加了很多这种操作。 要转换此操作有用(我的意思是快速和非常安全),你可以使用CE DataAdapter。

通过样品,没有关于按键护理,波纹管列出的步骤可以帮助U:

  1. 确保SORCE和目标表具有相同的字段结构;
  2. 从源数据库克隆带有数据表的虚拟数据表(您的选择);
  3. 使用表名作为commandtext(TableDirect as commandtype)创建一个CE命令;
  4. 从CE命令创建一个CE数据适配器;
  5. 从CE数据存储器创建一个CE命令生成器;
  6. 将来自CE commandbuilder的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都是较慢的数据类型。

P.S .:正如我所说的,这种方法不太在乎钥匙,所以要小心。

7

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

+0

它是否支持Visual Studio 2008? – soclose 2011-10-28 02:20:55

+0

是的 - 这是一个VS 2008解决方案。 – ErikEJ 2011-10-28 06:28:47

+0

要使用这个,我必须将所有五个.DLL添加到我的项目吗?或者只是其中的一部分?他们是替代“通用”SqlCe .DLL,还是他们是附加的? – 2013-11-20 00:51:06