How to convert datareader to datatable

I have a question about converting a datareader to datatable . In my code, I have a datareader created in one class and passed to another class that I want to convert to datatable .

When I do this, it does not work as the table remains empty. If I do the conversion in the same function, it works fine.

The only thing when I pass the datareader another function that stops working . Is it because dr closed or something else? How do I solve this problem? Any help would be great.

+6
source share
2 answers

Check this:

 Public Function ExecuteQuery(ByVal s As String, ByVal condb As SqlConnection, ByVal ParamArray params() As SqlParameter) As DataTable Dim dt As DataTable = Nothing Using da As New System.Data.SqlClient.SqlDataAdapter(s, condb) dt = New DataTable If params.Length > 0 Then da.SelectCommand.Parameters.AddRange(params) End If If da.SelectCommand.Connection.State <> ConnectionState.Open Then da.SelectCommand.Connection.Open() da.Fill(dt) End Using Return dt End Function 
+2
source

Use the DataTable Load() method.

  // Given a DataReader called "reader" DataTable dt = new DataTable(); dt.Load(reader) 
+39
source

Source: https://habr.com/ru/post/909177/


All Articles