Populate data table from data reader
You can load a DataTable
directly from a data reader using the Load()
method that accepts an IDataReader
.
var dataReader = cmd.ExecuteReader();
var dataTable = new DataTable();
dataTable.Load(dataReader);
Populate multiple datatables from DataReader. Is it even possible?
Very old question, but in case someone else stumbles upon this... Found the answer in this SO answer: If you use DataTable.Load()
then you should not use rdr.NextResult()
, as that is taken care implicitly. Simply call .Load()
for your next table. So, for example, if you have an expectedTableCount
:
ds = new DataSet();
ds.Tables.Add("Table1");
ds.Tables[0].Load(reader);
for (int ii = 1; ii < expectedTableCount; ii++)
{
ds.Tables.Add("Table" + (ii + 1));
ds.Tables[ii].Load(reader);
}
How to read data from a data reader into a data table?
Missing the users.Add(user);
at the end of each loop
private IList<User> GetUsersFromDataTable(DataTable usersTable)
{
IList<User> users = new List<User>();
foreach (DataRow row in usersTable.Rows)
{
User user = new User();
user.UserID = (int)row["UserID"];
user.Password = (string)row["Password"];
user.Username = (string)row["Username"];
users.Add(user);
}
return users;
}
How to convert a datareader to datatable
Check this out:
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
Trouble loading SQL Data Reader data into DataTable
Instead of a SqlDataReader
, use a SqlDataAdapter
.
SqlDataAdapter myAdapter = new SqlDataAdapter(command);
myAdapter.Fill(datatable);
With a SqlDataAdapter
, you don't need to explicitly call SqlConnection.Open()
and SqlConnection.Close()
. It is handled in the Fill()
method.
how read with datareader from datatable
Find a different control. That one is hard-coded to using an OdbcDataReader
. You don't use a DataReader
to read data from a DataTable
- you iterate through the DataRows
.
As a workaround, you could overload AddItems to accept a DataTable:
public void AddItems(DataTable dt, string textField, string valueField)
{
ClearAll();
int i = 0;
foreach (DataRow dr in dt.Rows)
{
chkList.Items.Add(dr[textField].ToString());
chkList.Items[i].Value = dr[valueField].ToString();
i++;
}
}
Convert datatable to datareader
I know this is old, but the answers here seem to have missed the point of the OPs question.
DataTables have a method called CreateDataReader which will allow you to convert a DataTable to a DbDataReader object. In this case a DataTableReader.
DataTable table = new DataTable();
//Fill table with data
//table = YourGetDataMethod();
DataTableReader reader = table.CreateDataReader();
I should point out that this will not increase performance since you should be using one or the other.
Here are some more resources on the matter:
- DataReader Vs DataTable
- Is datareader quicker than dataset when populating a datatable?
How to retrieve second resultset from DataReader to Datatable?
Until the reader is not closed you can load the further tables. If you have multiple results I suggest you to populate a DataSet
, which can contain multiple DataTable
instances:
public static DataSet ToDataSet(IDataReader reader)
{
DataSet ds = new DataSet();
while (!reader.IsClosed)
{
DataTable dt = new DataTable();
dt.Load(reader);
ds.Tables.Add(dt);
}
return ds;
}
Related Topics
On Postback, How to Check Which Control Cause Postback in Page_Init Event
Windows.Forms.Timer or System.Threading.Timer
Differencebetween Getter-Only Auto Properties and Expression Body Properties
Configure JSON.Net to Ignore Datacontract/Datamember Attributes
How to Handle Session End in Global.Asax
Convert.Changetype() Fails on Nullable Types
Facebook/ Twitter with Dotnetopenauth
How to Override an Existing Extension Method
Leave Only Two Decimal Places After the Dot
How to Add an Extra Button to the Window's Title Bar
"Updatesourcetrigger=Propertychanged" Equivalent for a Windows Phone 7 Textbox
Cannot Access a Disposed Object in ASP.NET Core When Injecting Dbcontext
"There Was an Error Running the Selected Code Generator" in VS 2013 Scaffolding
How to Handle JSON That Returns Both a String and a String Array
Fastest Way to Interface Between Live (Unsaved) Excel Data and C# Objects
How to Use the Linqpad Dump() Extension Method in Visual Studio