2009-10-22 32 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