views:

87

answers:

6

How to convert DataSet Value to DataReader?

A: 

You can't. If you want more explanation, please provide more information.

Thorsten Dittmar
+3  A: 

DataSet has a method called CreateDataReader which will create a DataTableReader, but I don't think that you can create a DataReader.

DataSet.CreateDataReader

Ardman
+1  A: 

DataSet is a disconnected in-memory object. DataReader is a connected unidirectional object.

So I guess it is not possible.

Is it really needed?

JMSA
A: 

If you want to iterate thru DataSet, you don't need a DataReader. DataSet is a disconnected in-memory object so iterate thru it using for-each:

foreach(var row in ds.Tables["YourTable"])
{
     var value = row.Field<int>("ID"); // etc
}
abatishchev
+1  A: 

Both DataSet and DataTable expose a method CreateDataReader which creates a DataTableReader. Check these links -

http://msdn.microsoft.com/en-us/library/system.data.dataset.createdatareader.aspx

http://msdn.microsoft.com/en-us/library/system.data.datatable.createdatareader.aspx

Sachin Shanbhag
A: 

You cannot convert a DataSet to a DbDataReader.

You can however create a DbDataReader that will read results from the DataSet by calling the CreateDataReader method on the DataSet.

However, this seems like a strange thing to want to do. You can simply iterate through the results contained in the DataSet using the Tables property of DataSet and The Rows property of DataTable. Using a DbDataReader would restrict you to forward only access to the results. The only benefit I can see from using a DbDataReader would be if you had an API call to make which required one as a parameter.

If your DataSet is the result of a SELECT command from a database, you should be able to get a DbDataReader by calling DbCommand.ExecuteReader(). This will cut out the DataSet altogether and result in something that is more efficient.

Alex Humphrey