I am trying to pass the OleDbDataReader to table that I can pass out of a function.

Dani AI

Generated

A short clarification and a few practical options building on the thread: ’s move to a disconnected container is the right idea for "returning a table" — a DataSet/DataTable can be returned safely because it does not require the connection to stay open. ’s warning about open readers on the same connection is also important: an open OleDbDataReader ties up its connection and will block other commands/readers on that connection.

When a disconnected result is wanted, load the reader into a DataTable and return that. This keeps the connection/reader local to the function and avoids resource leaks. Example pattern in VB:

Using conn As New OleDbConnection(connString)
    Using cmd As New OleDbCommand(sql, conn)
        conn.Open()
        Using rdr As OleDbDataReader = cmd.ExecuteReader()
            Dim dt As New DataTable()
            dt.Load(rdr)
            Return dt
        End Using
    End Using
End Using

If returning an open OleDbDataReader is absolutely required, use CommandBehavior.CloseConnection so closing the reader also closes the connection — but document and enforce that the caller must call Close/Dispose on the reader. Example pattern:

Dim conn As New OleDbConnection(connString)
Dim cmd As New OleDbCommand(sql, conn)
conn.Open()
Dim rdr As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return rdr

Final notes: prefer returning DataTable/DataSet for safety and simpler lifetime management; always use Using or try/finally to dispose connections/readers; avoid trying to run multiple readers on one OleDbConnection (that will fail unless separate connections are used).

I answered myself, if anyone needs

Dim myReader As New DataSet
myAdapt.Fill(myReader)
Return myReader

Just make sure to close the reader before opening any others on the same connection.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.