I am Executing One query using stored procedure object. i want to retreive number of rows returns in Select Statement after executing the query.
I am Confused between using ExecuteReader() & ExecuteScalar()
public static int getDuplicateEvent(string ATM, string Fault1, string Fault2, ref SqlConnection Connection)
{
string sQuery = "";
int result = 0;
try
{
sQuery = /*Query With Format Select Code From A Union Select Code From B */
using (SqlStoredProcedure sspObj = new SqlStoredProcedure(sQuery, Connection, CommandType.Text))
{
result = (int)sspObj.ExecuteScalar();
sspObj.Dispose();
}
}
catch (Exception xObj)
{
result = 0;
}
return result;
}
ExecuteScalarreturns the first column of the first row of the resultsExecuteReaderreturns a datareader that can be iterated throughYou could also use
ExecuteNonQueryorFillinto aDataSetorDataTableAssuming you want the rows and the count, I would fill a DataTable and count the rows using
Rows.Count