从 DataTable 批量插入到 SQLCE DataSource
这是一个使用 SQL CE 作为数据源的 C# WPF 应用程序:
我有一个 DataTable(显示为 DataGrid)和一个 SQL CE 数据源。 我使用 DataAdapter、DataSet 和 DataTable 从 SQL CE 填充 DataTable。然后将我的 DataGrid 绑定到 DataTable。
我可以将行(>10,000)行添加到我的数据表中,并且可以在将所有更改一起传播到我的 Sql CE 数据源之前编辑数据。
我当前的方法是 DROP TABLE、CREATE TABLE 并通过强力将行重新插入到 SQLCE。 SQL CE 没有批量插入,并且我不想使用第三个库或 dll。性能很慢...
我正在寻找一种更快的“批量插入”方法,而不需要逐一删除、创建和插入行。
我读了一些有关 SqlCeResultSet 的内容,但找不到任何文档,想知道它与我想做的事情有什么关系。
[编辑]
按照答案并检查这一点:
http://ruudvanderlinden.com/2010/ 10/13/bulk-insert-into-sql-ce-in-c/
我尝试使用该功能,但似乎不起作用。 下面是我的代码。 Footable 是我的数据库表,我有两列 - “id”和“FooName”。
Hashtable idHash = new Hashtable();
Hashtable fooNameHash = new Hashtable();
foreach(DataRow row in dt.Rows)
{
idHash.Add("id",row["id"]);
fooNameHash.Add("FooName",row["FooName"]);
}
List<Hashtable> colHashList = new List<Hashtable>();
colHashList.Add(idHash);
colHashList.Add(fooNameHash);
BulkInsertDatabase(colHashList, "FooTable");
它不起作用,但我在上面的代码中没有看到任何问题,所以希望有人能指出它..
[编辑 - 2nd][ANSWER]
最后我让代码可以工作(尽管性能值得怀疑):
List<Hashtable> colHashList = new List<Hashtable>();
Hashtable[] idHash = new Hashtable[dt.Rows.Count];
Hashtable[] fooNameHash = new Hashtable[dt.Rows.Count];
int i=0;
foreach(DataRow row in dt.Rows)
{
idHash[i] = new Hashtable();
idHash[i].Add("id", row["id"]);
colHashList.Add(idHash[i]);
fooNameHash[i] = new Hashtable();
fooNameHash[i].Add("FooName", row["FooName"]);
colHashList.Add(fooNameHash[i]);
i++;
}
BulkInsertDatabase(colHashList, "FooTable");
This an C# WPF application with SQL CE as DataSource:
I have a DataTable (display as DataGrid) and a SQL CE DataSource.
I populate my DataTable from SQL CE using DataAdapter, DataSet and DataTable. Then bind my DataGrid to the DataTable.
I may add rows (>10,000) rows to my DataTable and may have data edited before propagating all my changes all together to my Sql CE DataSource.
My current approach is DROP TABLE, CREATE TABLE, and re-INSERT rows by brute force to SQLCE. SQL CE has no bulk insert, and I do not want to use a 3rd library or dll. The performance is slow...
I am looking or a faster way to "bulk-insert" without the need to drop, create and insert row one-by-one.
I read something about SqlCeResultSet but I can't find any documentation and wonder does it has anything to do with what I'm trying to do.
[EDIT]
Following the answer and checking up on this:
http://ruudvanderlinden.com/2010/10/13/bulk-insert-into-sql-ce-in-c/
I tried to use the function but it didn't seem to work.
Below is my code. Footable is my database table and I have two columns - "id" and "FooName".
Hashtable idHash = new Hashtable();
Hashtable fooNameHash = new Hashtable();
foreach(DataRow row in dt.Rows)
{
idHash.Add("id",row["id"]);
fooNameHash.Add("FooName",row["FooName"]);
}
List<Hashtable> colHashList = new List<Hashtable>();
colHashList.Add(idHash);
colHashList.Add(fooNameHash);
BulkInsertDatabase(colHashList, "FooTable");
It didn't work but I don't see any problem in my above code so hope someone can point it out..
[EDIT - 2nd][ANSWER]
Finally I get the code to work (though doubtful with the performance):
List<Hashtable> colHashList = new List<Hashtable>();
Hashtable[] idHash = new Hashtable[dt.Rows.Count];
Hashtable[] fooNameHash = new Hashtable[dt.Rows.Count];
int i=0;
foreach(DataRow row in dt.Rows)
{
idHash[i] = new Hashtable();
idHash[i].Add("id", row["id"]);
colHashList.Add(idHash[i]);
fooNameHash[i] = new Hashtable();
fooNameHash[i].Add("FooName", row["FooName"]);
colHashList.Add(fooNameHash[i]);
i++;
}
BulkInsertDatabase(colHashList, "FooTable");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用 SqlCeResultSet 类< /a>
它允许在 SQL CE 数据库中批量导入 我在 10 秒内导入了 100000 行
查看示例
You should use the SqlCeResultSet Class
it is allow bulk import in SQL CE data base I've imported 100000 row in a 10 seconds
look at the example
您可以在此处使用我的批量插入库中的 C# 源代码:http://sqlcebulkcopy.codeplex.com
You can use the C# source code from my Bulk Insert Library here: http://sqlcebulkcopy.codeplex.com
它的工作,试试这个。
its work, try this.