是否可以在Sql Compact Edition中使用SqlBulkcopy (* .sdf)文件?使用sql的sqlbulkcopy CE
我知道它適用於SQL Server 200以上版本,但希望檢查CE兼容性。
如果它沒有其他人知道在沒有使用DataSets(puke here)的情況下將CSV類型文件導入SQL Server CE的最快方法?
是否可以在Sql Compact Edition中使用SqlBulkcopy (* .sdf)文件?使用sql的sqlbulkcopy CE
我知道它適用於SQL Server 200以上版本,但希望檢查CE兼容性。
如果它沒有其他人知道在沒有使用DataSets(puke here)的情況下將CSV類型文件導入SQL Server CE的最快方法?
不,我認爲SqlBulkCopy
不受支持(請參閱MSDN)。也許把數據放在xml中,然後在服務器上拆開它? SQL/XML在2005/2008非常好。
您可能也想看一下表值參數,但我懷疑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行每秒
寫入是可能增加了很多這種操作。 要轉換此操作有用(我的意思是快速和非常安全),你可以使用CE DataAdapter。
通過樣品,沒有關於按鍵護理,波紋管列出的步驟可以幫助U:
複製「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 .:正如我所說的,這種方法不太在乎鑰匙,所以要小心。
我在這裏有一個SqlCeBulkCopy庫:http://sqlcebulkcopy.codeplex.com - 甚至支持IEnumerable。
有趣的是,謝謝:) – leppie 2009-10-23 09:59:58
這種方法不支持標識和rowversion列類型 – 2011-09-13 04:57:36
哇,一直在努力與插入和獲得20-30行每秒,我敢肯定,這只是做了所有35,774行不到1第二(我所有的數據都在內存中的一個數組中),所以沒有來自源頭的瓶頸。 – Matt 2012-02-01 16:13:35