Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

C# Code is taking TOO LONG to run, Need to increase the speed of code.

Need to increase speed of code.  The below code works.  The problem is that I am receiving approximately 4,000 records from the Stored Procedure.  The below process is taking a long time to process.  I believe it is occurring at EnumValidation.FindFields.

Any idea how to speed up the process?
public List<T> SelectSPROC(string sprocName, List<Parameters> parameters) 
        {
            List<T> result = new List<T>();

            try
            {
                using (SQLConn = new SqlConnection(ConnectionStrings.GetConnectionString()))
                using (SQLCmd = new SqlCommand())
                {

                    SQLCmd.CommandText = sprocName;
                    SQLCmd.CommandType = CommandType.StoredProcedure;
                    SQLCmd.Connection = SQLConn;
                    SQLCmd.CommandTimeout = 0;

                    if (parameters.Count > 0)
                    {
                        for (int i = 0; i < parameters.Count; i++)
                        {
                            SQLCmd.Parameters.AddWithValue(parameters[i].parameterName, parameters[i].parameterValue);
                        }
                    }

                    SQLConn.Open();

                    SQLRdr = SQLCmd.ExecuteReader();

                    while (SQLRdr.Read())
                    {
                        T t = new T();  

                        for (int inc = 0; inc < SQLRdr.FieldCount; inc++)
                        {
                            Type type = t.GetType();
                            string column = SQLRdr.GetName(inc);
                            //Console.WriteLine(column);
                            if (!EnumValidation.FindFields<Enumerators.ExcludedFields>(column))  //SEEMS TO SLOW DOWN HERE
                            {
                                PropertyInfo prop = type.GetProperty(column);
                                string value = string.Empty;
                                if (ServiceDate.IsDate(SQLRdr.GetValue(inc).ToString()) &&
                                    !SQLRdr.GetValue(inc).ToString().Contains('.') &&
                                    EnumValidation.FindFields<Enumerators.DateFields>(column))
                                {
                                    try
                                    {
                                        DateTime dt = Convert.ToDateTime(SQLRdr.GetValue(inc));
                                        value = dt.ToString("yyyyMMdd");
                                    }
                                    catch (Exception ex)
                                    {
                                        string err = ex.Message;
                                    }
                                }
                                else
                                {
                                    value = SQLRdr.GetValue(inc).ToString();
                                }
                                prop.SetValue(t, value, null);
                            }
                        }

                        result.Add(t);
                        Console.WriteLine(result.Count);
                    }
                    SQLRdr.Close();
                }
            }
            catch (Exception ex)
            {
                filename = AppSettings.GetExportLocationArchive() + AppSettings.GetExportErrorFileName();
                ExportResult.WriteError(ex.ToString(), filename);
                Environment.Exit(1);
            }
            finally
            {
                SQLConn.Close();
                SQLCmd.Dispose();
            }

            return result;
        }

Open in new window

public static class Enumerators
    {
        public enum ExcludedFields
        {
            Status,
            distributorName,
            notes,
            revenueCategory
        }

        public enum DateFields
        {
            dateOrder,
            requiredDate,
            promisedDate,
            shipDate
        }
    }

Open in new window

//This class is where it seems to slow down.
    public static class EnumValidation
    {
        public static bool FindFields<T>(string value)
        {
            try
            {
                if (typeof(T).IsEnum)
                {
                    T column = (T)Enum.Parse(typeof(T), value);
                    if (Enum.IsDefined(typeof(T), column))
                        return true;
                    else
                        return false;
                }
                else
                {
                    throw new ApplicationException("Enum is not valid");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }

Open in new window

    public class Parameters
    {
        public string parameterName { get; set; }
        public string parameterValue { get; set; }
    }

Open in new window

SOLUTION
Avatar of Arana (G.P.)
Arana (G.P.)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CipherIS

ASKER

How do you use CodeTrack to figure out the issue?
codetrack.JPG
Ok
FindFields takes 2 m 36 s
DateTime.TryParse (IsDate) takes 1 m 40 s

Those 2 areas seem to be slowing the app.

Any idea how to increase the speed?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Code is the solution to increase the speed.  I was going to selected assisted solution but didn't see that available.  Thanks for providing info to help me resolve the issue.