Sql Compact Edition などの (*.sdf) ファイルで SqlBulkcopy を使用することは可能ですか?
SQL Server 200 Up で動作することはわかっていますが、CE の互換性を確認したかったのです。
DataSets を使用せずに CSV タイプのファイルを SQL Server CE に取り込む最速の方法を誰も知らない場合 (ここで puke)?
Sql Compact Edition などの (*.sdf) ファイルで SqlBulkcopy を使用することは可能ですか?
SQL Server 200 Up で動作することはわかっていますが、CE の互換性を確認したかったのです。
DataSets を使用せずに CSV タイプのファイルを SQL Server CE に取り込む最速の方法を誰も知らない場合 (ここで puke)?
BULKCOPYはSQLCEではサポートされていません。テーブルに膨大な数の行がある場合は、これが最速の方法です。挿入が遅すぎます!
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 を使用できます。
サンプルでは、キーを気にする必要はありません。以下にリストされている手順が役立ちます。
次のようにして、「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 行のテーブルで、平均レートは 1 秒あたり約 1500 行です。
もちろん、すべてはテーブルの構造に依存します。IMAGE と NTEXT はどちらも遅いデータ型です。
PS: 私が言ったように、このメソッドはキーをあまり気にしないので、注意してください。
SqlBulkCopy
いいえ、サポートされていないと思います( MSDNを参照)。おそらく、データをxmlとしてスローし、サーバーで分解しますか?SQL / XMLは、2005/2008ではかなり優れています。
table-value-parametersも確認することをお勧めしますが、CEがこれらをサポートしているとは思えません。