lidds Posted January 3, 2006 Posted January 3, 2006 (edited) Determine number of rows in oledbdatareader... I need to find out how many rows are in my datareader. The code is as follows: Dim myReader As OleDb.OleDbDataReader = Nothing Dim myCmd As New OleDb.OleDbCommand("spQryIssue") myCmd.CommandType = CommandType.StoredProcedure myReader = myDB.RunMyDataQuery(myCmd) Is there something like myReader.rows.count() I know you can do Do While myReader.Read but I physically need the number Thanks Simon Edited January 3, 2006 by lidds Quote
Mister E Posted January 3, 2006 Posted January 3, 2006 I have not tried this, so you will have to give it a shot:OleDbCommand cmd = new OleDbCommand(); cmd.CommandText = "spQryIssue"; cmd.CommandType = CommandType.StoredProcedure; //cmd.Connection = // some OleDbConnection OleDbDataAdapter da = new OleDbDataAdapter(); da.SelectCommand = cmd; DataSet ds = new DataSet(); da.Fill(ds); foreach (DataTable dt in ds.Tables) { Console.Write("ROW COUNT: " + dt.Rows.Count); }Basically you want to use a OleDbDataAdapter to fill a DataSet. Then access the individual DataTable objects in the DataSet. Quote
lidds Posted January 3, 2006 Author Posted January 3, 2006 Thanks, did not expect it to be that much hassle :) Just out of interest there is probably only going to be no more than 10 rows in the dataReader, would it be quicker to loop through and count them rather than to do the DataSet route???? Thanks for your help Simon Quote
Mister E Posted January 3, 2006 Posted January 3, 2006 Well, coding that is going to probably be a bigger hassle than what I posted. Plus, getting your results into a DataSet is a lot more useful throughout .NET. Quote
Administrators PlausiblyDamp Posted January 4, 2006 Administrators Posted January 4, 2006 What does your SQL look like? You could always use an output parameter in your stored proc to return @@rowcount or similar. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
HJB417 Posted January 4, 2006 Posted January 4, 2006 The only problem with using datareaders is, I believe the values of the parameters aren't set until the datareader is closed. Quote
lidds Posted January 4, 2006 Author Posted January 4, 2006 (edited) my sql is now SELECT * FROM myTable SELECT @@ROWCOUNT AS rowNum But how do I get the rowcount value?? I have tried the following but does not seem to work. MsgBox(myReader.Item("rowNum").ToString()) Thanks for all your help guys Simon Edited January 4, 2006 by lidds Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.