从数据表中批量插入到数据源SQLCE [英] Bulk insert from DataTable to SQLCE DataSource

查看:909
本文介绍了从数据表中批量插入到数据源SQLCE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是C#WPF应用程序与SQL CE作为数据源:



我有一个数据表(显示为DataGrid中)和SQL CE数据源。
我用填充DataAdapter的,DataSet和DataTable我从SQL CE数据表。然后绑定我DataGrid中的数据表。



我可以添加行(> 10,000)行到我的DataTable,并可能有我的所有更改传播都在一起,我的SQL数据之前编辑CE数据源。



我目前的做法是DROP TABLE,CREATE TABLE和蛮力重新插入行SQLCE。 SQL CE没有批量插入,我不希望使用第三个库或DLL。性能很慢...



我要寻找或速度更快的方式来批量插入无需降,创建并插入一个接一个行



我读了一些有关SqlCeResultSet,但我找不到任何文档和难怪它有什么做什么,我试图做的。





继答案,并在此核对:

http://ruudvanderlinden.com/2010/10/13/批量插入 - 到 - SQL-CE-在C /



我想使用的功能,但它似乎没有工作。
下面是我的代码。 Footable是我的数据库表,我有两列 - id和FooName

  Hashtable的idHash =新的Hashtable() ; 
Hashtable的fooNameHash =新的Hashtable();
的foreach(DataRow的行dt.Rows)
{
idHash.Add(ID,行[ID]);
fooNameHash.Add(FooName,行[FooName]);
}

名单,LT; Hashtable的> colHashList =新的List< Hashtable的>();
colHashList.Add(idHash);
colHashList.Add(fooNameHash);

BulkInsertDatabase(colHashListFooTable);



它没有工作,但我没有看到我上面的代码中的任何问题,所以希望有人能指出这一点。



[答案]

最后我得到的代码工作(虽然怀疑与性能):

 列表< Hashtable的> colHashList =新的List< Hashtable的>(); 

哈希表[] = idHash新的Hashtable [dt.Rows.Count]
哈希表[] = fooNameHash新的Hashtable [dt.Rows.Count]

INT I = 0;
的foreach(DataRow的行dt.Rows)
{
idHash [i] =新的Hashtable();
idHash [I]。新增(ID,行[ID]);
colHashList.Add(idHash [I]);

fooNameHash [i] =新的Hashtable();
fooNameHash [I]。新增(FooName,行[FooName]);
colHashList.Add(fooNameHash [I]);

I ++;
}

BulkInsertDatabase(colHashListFooTable);


解决方案

您应该使用的 SqlCeResultSet类



这是允许SQL CE数据库中批量导入我已经导入100000行10秒作为一个
看的例如


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");

解决方案

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

这篇关于从数据表中批量插入到数据源SQLCE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆