sqlbulkcopy using sql CE

前端 未结 4 2071
天命终不由人
天命终不由人 2020-11-30 07:12

Is it possible to use SqlBulkcopy with Sql Compact Edition e.g. (*.sdf) files?

I know it works with SQL Server 200 Up, but wanted to check CE compatibility.

4条回答
  •  不知归路
    2020-11-30 07:33

    Is possible to increase a lot this kind of operation. To turn this operation usefull (I mean fast and pretty safe), u can use CE DataAdapter.

    By sample, no care about keys, the steps listed bellow can help u:

    1. Make sure that sorce and target tables have same fields structure;
    2. Clone a virtual datatable with a datatable from source database (your select);
    3. Create a CE command with the table name as commandtext (TableDirect as commandtype);
    4. Create a CE dataadapter from CE command;
    5. Create a CE commandbuilder from CE dataatapter;
    6. Pass the Insert command from CE commandbuilder to CE dataadapter;
    7. Copy "n" batch rows from your source datatable to the target datatable (the clone), doing something like this:

      '... 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
      

    With this way u can update several rows with no much time.

    I've some applications using this method and the average rate is about 1500 rows per second in a table with 5 NTEXT fields (slow) and 800000 rows.

    Of course, all depends of your table's structure. IMAGE and NTEXT are both slow datatypes.

    P.S.: As I said, this method don't care so much about keys, so be carefull.

提交回复
热议问题