OleDbDataAdapter更新问题 [英] OleDbDataAdapter Update question
问题描述
我有一个包含很多行(超过一亿行)的DataTable,并且正在编写一个需要插入该表的应用程序.
I have a DataTable with a lot of rows (Over a hundred million) and am writing an application that needs to insert into that table.
我将使用OleDbDataAdapter来完成这项工作,我不知道什么是最好的方法.我只需要插入到这个巨大的表中,但是我不想将插入语句硬编码到应用程序中.
I will be using OleDbDataAdapter for the job and I am puzzled whats the best way to do this. I only need to insert into this enormous table, however I don't want to hard code the insert statement into application.
我认为我可以使用
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand("select * from table_name");
OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
...
adapter.Fill(data_set_name, "Table_name");
但是这真的很不好,因为我不需要/想要数据,而且内存使用情况也很糟糕.所以我很感兴趣是否可以用TOP更改SelectCommand?看起来像这样:
But this would be really bad since I don't need/want the data and the memory usage would be awful. So I was interesting if I could alter SelectCommand with TOP? It would look like so:
adapter.SelectCommand = new OleDbCommand("select TOP 1 * from table_name");
现在,填充"命令将非常快,并且我将拥有以后所有插入语句所需的数据.我可以将行添加到数据表中,然后调用
Now the Fill command would be really fast and I would have the data I needed for all the future insert statements. I could add rows to datatable and then just call
adapter.Update(data_set_name, "Table_name");
这项工作吗?这是这样做的有效/推荐方法吗?应用程序必须快速且仅使用必要的资源,这一点非常重要.有更好的方法吗?
Would this work? And is this a valid / recommended way of doing this? It is really important that the application is fast and uses only the necessary resources. Is there a better way of doing this?
谢谢您的输入!
推荐答案
IMO,最好的方法是:
IMO, the best way would be to:
-
使用
OleDbDataAdapter.FillSchema(data_set_name, SchemaType.Source)
方法创建具有从数据源映射的结构的DataTable.您基本上是通过在Select语句中拉一行来尝试做同样的事情.在这种情况下,您的Select语句可以保留为"select * from table_name
".我相信您现在不需要调用OleDbDataAdapter.Fill
方法.
Use the
OleDbDataAdapter.FillSchema(data_set_name, SchemaType.Source)
method to create the DataTable with a structure mapped from the datasource. You are basically trying to do the same thing by pulling a single row in your Select statement. Your Select statement in this case could remain "select * from table_name
". I believe that you do not need to call theOleDbDataAdapter.Fill
method now.
您可以自己创建InsertCommand
语句,而不是使用CommandBuilder
.
Instead of using a CommandBuilder
, create your InsertCommand
statement yourself.
这篇关于OleDbDataAdapter更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!