BilalAhmed
BilalAhmed

Reputation: 191

Easy way to find Query return how many rows SQL Server

This is windows back-end service where I need to write each and every action to Log file. I want to find how many rows return by SQL Select Query, I want to write that record to Log file.

Example:

Select statement return 35 rows.

Logs entries:

4/18/2016 4:54:47 PM : -Class--> CallBackOption| -Function--> UpdateServiceIBC| -Message--> SELECT * FROM CALLBACKOPTION WHERE ProccessStatus = 0
4/18/2016 4:54:47 PM : -Class--> Update Service Call Back Option| -Function--> UpdateServiceCall Back Option| -Message--> ***** New Record *****
4/18/2016 4:54:47 PM : -Class--> Update Service Call Back Option| -Function--> UpdateServiceCall Back Option| -Message--> Formatting ANI[sip:[email protected]] TO Contact[03022745301] against CA[400001358877]
4/18/2016 4:54:47 PM : -Class--> Update Service Call Back Option| -Function--> UpdateServiceCall Back Option| -Message--> Checking for Contact Number
4/18/2016 4:54:47 PM : -Class--> Update Service Call Back Option| -Function--> UpdateServiceCall Back Option| -Message--> Contact Number[9903022745301] call process Chain ID[1]
4/18/2016 4:54:47 PM : -Class--> Update Service Call Back Option| -Function--> UpdateServiceCall Back Option| -Message--> Updating AutoCall Back Option

Code Snippet:

     string query = "SELECT * FROM CALLBACKOPTION WHERE ProccessStatus = 0";
                    Log.WriteErrorLog("CallBackOption", "UpdateServiceIBC", "SELECT * FROM CALLBACKOPTION WHERE ProccessStatus = 0");
                    using (SqlConnection con = new SqlConnection(ConnectionString))
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand(query, con);
                        SqlDataReader reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            Log.WriteErrorLog("Update Service Call Back Option", "UpdateServiceCall Back Option", "***** New Record *****");
                            string dateTime = reader["DateTime"].ToString();
                            string ANI = reader["ANI"].ToString();
                            string contact = ANI.Substring(ANI.IndexOf(":") + 1, ANI.IndexOf("@") - 4);
                            string CA = reader["CUSTOMER_ACCOUNT"].ToString();
Log.WriteErrorLog("","", "Formatting ANI[" + ANI + "] TO Contact[" + contact + "] against CA[" + CA + "]");

Upvotes: 1

Views: 99

Answers (2)

Gottfried Lesigang
Gottfried Lesigang

Reputation: 67311

The easiest way in my eyes (if you are reading the data in any case):

[...]
int counter=0;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()){
    counter++;
    [...]
}

The variabe counter will show the count of rows being processed.

Are there other CRUD statements too?

Upvotes: 2

Leonard Klausmann
Leonard Klausmann

Reputation: 215

If you want to know only the amount of rows you can use:

cmd.ExecuteNonQuery();

That will executes an SQL statement against the Connection and returns the number of rows affected.

MSDN

Upvotes: 0

Related Questions