如何使用DataReader读取数据库记录并将其添加到DataTable

时间:2010-03-22 15:07:05

标签: database ado.net extract datareader

我在Oracle数据库表(大约400万条记录)中有一些数据,我希望使用ADO.NET将其转换并存储在MSSQL数据库中。到目前为止,我使用(对于小得多的表)DataAdapter从Oracle DataBase读取数据并将DataTable添加到DataSet以进行进一步处理。

当我用我的巨大桌子尝试这个时,有一个outofmemory异常抛出。 (我假设这是因为我无法将整个表加载到我的记忆中):)

现在我正在寻找一种执行此提取/传输/加载的好方法,而不将整个表存储在内存中。我想使用DataReader并读取DataTable中的单个dataRecords。如果其中有大约100k行,我想处理它们并在之后清除DataTable(再次获得空闲内存)。

现在我想知道如何使用ado.net将单个datarecord作为行添加到dataTable,以及如何完全清除dataTable内存:到目前为止我的代码:

Dim dt As New DataTable

    Dim count As Int32
    count = 0
    ' reads data records from oracle database table'
    While rdr.Read()

        'read n records and add them to a dataTable'
        While count < 10000
            dt.Rows.Add(????)

            count = count + 1
        End While

        'transform data in the dataTable, and insert it to the destination'


        ' flush the dataTable after insertion'
        count = 0
    End While

非常感谢您的回复!

1 个答案:

答案 0 :(得分:1)

您可以使用原始方法,但在select语句中使用limitskip批量执行此操作。因此,您一次只能执行100,000行并循环,直到获得所有数据。

这样你就不必太多地改变你的代码了