Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an sql C# program that has the following code which slows down a lot after calling the function over 1,000 times. Intially it only takes only 100 milliseconds for each iteration but after a lot of iterations, each iteration seems to take 1-2 seconds.
Can you let me know what could be wrong?

I don't get any error on the console output " Error Marking STB " , so its not the 1 second sleep.



C#
using (SqlConnection connection = new SqlConnection(connectionString))
{
    try
    {
        connection.Open();
    }
    catch
    {

        connection.Close();

        return 5;
    }
    string headend_query = "Headend1";
   

    string query3 = string.Format(@"UPDATE [STB_Details] SET [Delete_Status]= @status WHERE ([Serial_Number]= @serial_number AND [Status]='FINISHED' AND [TimeofTansaction_End]<=@end_time  AND [Error_Code]=0 AND  [Delete_Status]!=@status) ", headend_query);
    
        SqlCommand cmd = new SqlCommand(query3, connection); 
        cmd.CommandType = System.Data.CommandType.Text;   

        try
        {
            cmd.Parameters.Add(new SqlParameter("@status", SqlDbType.NVarChar)).Value = status;
        }
        catch
        {
            connection.Close();
            return 33331;
        }
        try
        {
            cmd.Parameters.Add(new SqlParameter("@date_time", SqlDbType.DateTime)).Value = DateTime.Now;
        }
        catch
        {
            connection.Close();
            return 33332;
        }
        try
        {
            cmd.Parameters.Add(new SqlParameter("@serial_number", SqlDbType.NVarChar)).Value = serial_number;
        }
        catch
        {
            connection.Close();
            return 33333;
        }
        try
        {
            cmd.Parameters.Add(new SqlParameter("@end_time", SqlDbType.DateTime)).Value = end_time;
        }
        catch
        {
            connection.Close();
            return 33334;
        }
        try
        {
            int rowsAffected = cmd.ExecuteNonQuery();

        }
        catch (Exception ex)
        {

            Console.WriteLine(" Error Marking STB");
            
            try
            {
                Thread.Sleep(1000);
                int rowsAffected = cmd.ExecuteNonQuery();
               
            }
            catch
            {
               
                errormessage = ex.Message + System.Environment.NewLine + ex.InnerException + System.Environment.NewLine + " Status- " + status + " Serial Number " + serial_number + " EndTime- " + end_time.ToString();
                connection.Close();
                return 6;
            }

        }
        connection.Close();


 }
Posted
Comments
Bernhard Hiller 15-Apr-13 2:38am    
using(SQLCOnnection ...) will close and dispose the connection object automatically when the using statment is left, also in case of errors.
Rerturn codes for different error conditions are a programming style of 1980ies - better use Exceptions, they'll also provide more information on what went wrong.
For your problem proper, I'd suggest to add some Logging statements just to find out which of the steps takes too much time: connection.open or cmd.ExecuteNonQuery
nitrous_007 15-Apr-13 9:31am    
You mean return an exception object? instead of an integer or just use it get error information

First, I'm not sure why you should be calling this a 1000 times.
Second, if your requirement is such that you have to do so and there is no alternative, then establish the database connection outside of the method.
Establishing the connection in every single call is an expensive overhead.

Try and call a set of updates together rather than calling every instance of update individually. This will require refactoring of your code.
 
Share this answer
 
C#
protected void MainMethod()
{
    // This is main mathod

    // Create connectionstring
    string connectionString = string.Empty;

    // Opening the connectionstring
    SqlConnection connection = new SqlConnection(connectionString);
    connection.Open();

    // Calling submethod to call the insertion 1000 times
    try
    {
        for(int i=1;i<=1000;i++){
            SaveData1000Times(connection);
        }
    }
    catch
    {
        // If something goes wrong anywhere, you should be returning custom errors based on error code/message
        string errrMessage = "Something wrong";
    }
    finally
    {
        // Finally closing the connection
        if(connection.State == System.Data.ConnectionState.Open)
            connection.Close();
    }
}



C#
// This is submethod. Note that there is no try catch block used as it should return error back to caller
        protected void SaveData1000Times(SqlConnection connection)
        {
            using (connection)
            {

                    if(connection.State != System.Data.ConnectionState.Open)
                        connection.Open();

                    string headend_query = "Headend1";
                    string query3 = string.Format(@"UPDATE [STB_Details] SET [Delete_Status]= @status WHERE ([Serial_Number]= @serial_number AND [Status]='FINISHED' AND [TimeofTansaction_End]<=@end_time  AND [Error_Code]=0 AND  [Delete_Status]!=@status) ", headend_query);
                    SqlCommand cmd = new SqlCommand(query3, connection);
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.Parameters.Add(new SqlParameter("@status", SqlDbType.NVarChar)).Value = status;
                    cmd.Parameters.Add(new SqlParameter("@date_time", SqlDbType.DateTime)).Value = DateTime.Now;
                    cmd.Parameters.Add(new SqlParameter("@serial_number", SqlDbType.NVarChar)).Value = serial_number;
                    cmd.Parameters.Add(new SqlParameter("@end_time", SqlDbType.DateTime)).Value = end_time;
                    int rowsAffected = cmd.ExecuteNonQuery();

        }



Hope this help you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900