Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# how do you return dataset from sqldatareader?

Tags:

c#

.net

winforms

I have this in a public class:

SqlConnection myConnection = new SqlConnection("Data Source=hermes;database=qcvalues; Integrated Security=SSPI;");
myConnection.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand(InitializeQuery(), myConnection);
myReader = myCommand.ExecuteReader();

I need the datasource of a control to get the dataset from myReader.

Unfortunately this is difficult to do because the control is on a form (a separate class). how would I return myReader dataset into the datasource property of the control on my form?

like image 469
Alex Gordon Avatar asked Nov 04 '10 17:11

Alex Gordon


2 Answers

You don't. Use a DataAdapter instead:

var ds = new DataSet();

using(var conn = new SqlConnection(connString))
{
    conn.Open();
    var command = new SqlCommand(InitializeQuery(), conn);
    var adapter = new SqlDataAdapter(command);

    adapter.Fill(ds);
}
like image 97
Justin Niessner Avatar answered Nov 10 '22 16:11

Justin Niessner


if you can use DataAdapter subclass or use something as:

DataTable myTable = new DataTable();

myTable.Load(myCommand.ExecuteReader());

and then return DataTable to client.

like image 25
pyCoder Avatar answered Nov 10 '22 18:11

pyCoder