![]() |
|
|
|
#1 |
|
Does anyone have any insight on how I can get a count of the records returned
after executing an ExecuteReader command? Something similar to the ..RecordCount property in Classic ASP? =?Utf-8?B?UGF1bA==?= |
|
|
|
|
#2 |
|
Posts: n/a
|
"Paul" <> wrote in message
news:7AD0ED33-BB76-4D13-8BBD-... > Does anyone have any insight on how I can get a count of the records > returned > after executing an ExecuteReader command? Something similar to the > .RecordCount property in Classic ASP? Not natively. The DataReader is a connected object, so its record count is not known until all the records have been read. If you don't need to know the record count *before* you start reading the records, you can simply increment a counter each time you read a record. If you just want to know whether the DataReader contains records or not, use its HasRows property (assuming you're using v1.1 of the Framework) However, if you really need to know the number of records before you have read them all, essentially you have two choices: 1) Return two recordsets, the first containing the number of rows e.g. SELECT COUNT(*) FROM <table> SELECT * FROM <table> 2) Use a DataSet... Mark Rae |
|
|
|
#3 |
|
Posts: n/a
|
declare 1 integer and assign the Datareader.ExecuteReader();
This method will return some integer(how many records affected / inserted) , work on that. int i=Datareader.ExecuteReader(); --try ? Mahesh www.snipurl.com/guac "Paul" <> wrote in message news:7AD0ED33-BB76-4D13-8BBD-... > Does anyone have any insight on how I can get a count of the records returned > after executing an ExecuteReader command? Something similar to the > .RecordCount property in Classic ASP? ¿ Mahesh Kumar |
|
|
|
#4 |
|
Posts: n/a
|
Thanks for this but the ExecuteReader method does not return an integer?
It is my understanding that it returns a SQLDataReader object. "¿ Mahesh Kumar" wrote: > declare 1 integer and assign the Datareader.ExecuteReader(); > This method will return some integer(how many records affected / inserted) , > work on that. > int i=Datareader.ExecuteReader(); --try ? > > Mahesh > www.snipurl.com/guac > > "Paul" <> wrote in message > news:7AD0ED33-BB76-4D13-8BBD-... > > Does anyone have any insight on how I can get a count of the records > returned > > after executing an ExecuteReader command? Something similar to the > > .RecordCount property in Classic ASP? > > > =?Utf-8?B?UGF1bA==?= |
|
|
|
#5 |
|
Posts: n/a
|
Thank you for this help.
I do need the count before I loop through the records. HasRows will not help because I need a specific count. Submitting 2 SQL calls (SELECT COUNT(*) and regular SELECT) is what I thought I might have to do, but was hoping for something more efficient. Using a DataSet rather than a DataReader might be the answer, but I still do not see any Count property that would give me the count before I looped through the records? "Mark Rae" wrote: > "Paul" <> wrote in message > news:7AD0ED33-BB76-4D13-8BBD-... > > > Does anyone have any insight on how I can get a count of the records > > returned > > after executing an ExecuteReader command? Something similar to the > > .RecordCount property in Classic ASP? > > Not natively. The DataReader is a connected object, so its record count is > not known until all the records have been read. > > If you don't need to know the record count *before* you start reading the > records, you can simply increment a counter each time you read a record. > > If you just want to know whether the DataReader contains records or not, use > its HasRows property (assuming you're using v1.1 of the Framework) > > However, if you really need to know the number of records before you have > read them all, essentially you have two choices: > > 1) Return two recordsets, the first containing the number of rows e.g. > > SELECT COUNT(*) FROM <table> > SELECT * FROM <table> > > 2) Use a DataSet... > > > =?Utf-8?B?UGF1bA==?= |
|
|
|
#6 |
|
Posts: n/a
|
"Paul" <> wrote in message
news:B008E19F-FAD6-44FB-B8B5-... > Using a DataSet rather than a DataReader might be the answer, but I still > do > not see any Count property that would give me the count before I looped > through the records? <DataSet object>.Tables[0].Rows[0].Count Mark Rae |
|
|
|
#7 |
|
Posts: n/a
|
"¿ Mahesh Kumar" <> wrote in message
news:u%... > declare 1 integer and assign the Datareader.ExecuteReader(); > This method will return some integer(how many records affected / inserted) > , > work on that. > int i=Datareader.ExecuteReader(); --try ? 1) ExecuteReader() is a method of the Command object, not the DataReader object. 2) ExecuteReader() returns a DataReader object, not an integer. Mark Rae |
|
|
|
#8 |
|
Posts: n/a
|
"Paul" <> wrote in message
news:922F8088-128F-4AB6-A999-... > Thanks for this but the ExecuteReader method does not return an integer? > > It is my understanding that it returns a SQLDataReader object. That's correct - you should ignore that post - it's totally wrong. Mark Rae |
|
|
|
#9 |
|
Posts: n/a
|
"Mark Rae" <> wrote in message
news:... > <DataSet object>.Tables[0].Rows[0].Count Woops, that should be DataSet object>.Tables[0].Rows.Count Mark Rae |
|
|
|
#10 |
|
Posts: n/a
|
There a many ways to do that.
You can make use of "ExecuteScalar " if you use Select count(*) from TableName you will get back a single item - - so you can use ExecuteScalar, instead of executeReader: dim counter as integer counter=cmdMbr.ExecuteScalar label1.text=counter Using Dataset also is a good idea but there is alos some burden vs getting the count(*) directly using command object Patrick "Mark Rae" <> wrote in message news:##5p#... > "¿ Mahesh Kumar" <> wrote in message > news:u%... > > > declare 1 integer and assign the Datareader.ExecuteReader(); > > This method will return some integer(how many records affected / inserted) > > , > > work on that. > > int i=Datareader.ExecuteReader(); --try ? > > 1) ExecuteReader() is a method of the Command object, not the DataReader > object. > > 2) ExecuteReader() returns a DataReader object, not an integer. > > Patrick.O.Ige |
|