是否可以在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