1

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 + "]");
4
  • Count in the loop? Its unclear what your asking. Commented Apr 18, 2016 at 12:16
  • Are you interested in the data itself or do you just need the count? Commented Apr 18, 2016 at 12:17
  • I just need the count....! Please see the edited question for your reference...! Commented Apr 18, 2016 at 12:19
  • Your question still shows you're using the data.. Commented Apr 18, 2016 at 12:23

2 Answers 2

2

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.