Click here to Skip to main content
15,883,860 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
Hi, I have a mySQL server hosting on google cloud and want to fetch records from that server, below is the method I am using. So my question is that is there any better way of doing so. Thanks in advance.

C#
public List<GetCarrierResult> GetCarrier(System.Nullable<int> p_BillPaymentProviderID, System.Nullable<int> p_CarrierID)
{
   try
   {
      List<GetCarrierResult> records = new List<GetCarrierResult>();

      if (Connection.State != ConnectionState.Open)
      {
         Connection.Open();
      }

      Command = new MySqlCommand("GetCarrier", Connection);
      Command.CommandType = CommandType.StoredProcedure;

      //Add Parameters if procedures requires.
      Command.Parameters.Add(new MySqlParameter("p_BillPaymentProviderID", p_BillPaymentProviderID));
      Command.Parameters.Add(new MySqlParameter("p_CarrierID", p_CarrierID));

      if (Transaction != null)
      {
         Command.Transaction = Transaction;
      }

      da = new MySqlDataAdapter();
      da.SelectCommand = Command;

      ds = new DataSet();
      da.Fill(ds);

      foreach (DataRow Row in ds.Tables[0].Rows)
      {
         GetCarrierResult result = new GetCarrierResult()
         {
            StoreID = Convert.ToInt32(Row["StoreID"]),
            CarrierID = Convert.ToInt32(Row["CarrierID"]),
            CarrierName = Row["CarrierName"] == DBNull.Value ? null : (string)Row["CarrierName"],
            MasterCarrierID = Convert.ToInt32(Row["MasterCarrierID"]),
            MasterCarrierName = Row["MasterCarrierName"] == DBNull.Value ? null : (string)Row["MasterCarrierName"],
            BillPaymentProviderID = Convert.ToInt32(Row["BillPaymentProviderID"]),
            ProviderName = Row["ProviderName"] == DBNull.Value ? null : (string)Row["ProviderName"],
            ProductID = Convert.ToInt32(Row["ProductID"]),
            ProductName = Row["ProductName"] == DBNull.Value ? null : (string)Row["ProductName"],
            ApplyTax1 = Convert.ToBoolean(Row["ApplyTax1"]),
            IsPaymentTax = Convert.ToBoolean(Row["IsPaymentTax"]),
            PaymentTaxRate = Convert.ToDecimal(Row["PaymentTaxRate"]),
            IsExtraTax = Convert.ToBoolean(Row["IsExtraTax"]),
            ExtraTaxType = Convert.ToInt32(Row["ExtraTaxType"]),
            ExtraTaxName = Row["ExtraTaxName"] == DBNull.Value ? null : (string)Row["ExtraTaxName"],
            ExtraTaxValue = Convert.ToDecimal(Row["ExtraTaxValue"]),
            IsFee = Convert.ToBoolean(Row["IsFee"]),
            FeeProductID = Row["FeeProductID"] == DBNull.Value ? (Int32?)null : Convert.ToInt32(Row["FeeProductID"]),
            FeeProductName = Row["FeeProductName"] == DBNull.Value ? null : (string)Row["FeeProductName"],
            FeeType = Convert.ToInt32(Row["FeeType"]),
            FeeValue = Convert.ToDecimal(Row["FeeValue"]),
            FeeCostType = Convert.ToInt32(Row["FeeCostType"]),
            FeeCostValue = Convert.ToDecimal(Row["FeeCostValue"]),
            EmployeeID = Convert.ToInt32(Row["EmployeeID"]),
            FullName = Row["FullName"] == DBNull.Value ? null : (string)Row["FullName"],
            WebAddress = Row["WebAddress"] == DBNull.Value ? null : (string)Row["WebAddress"],
         };

         records.Add(result);
      }

      return records;
   }
   catch
   {
      throw;
   }
}


Anything that could be done to improve speed?

tabulation reduced
Posted
Updated 8-Mar-14 10:07am
v3
Comments
CHill60 8-Mar-14 8:57am    
Not posting this as a solution because it's not necessarily "better" ...
You have several variables that are declared outside this function - better to have them in here or passed in (e.g. da, ds, Transaction, Connection)
In your catch clause you are just throwing any exceptions - are you sure you mean to do this (i.e. have the calling code handle all exceptions)
You have to do an lot of null checks there - would it be better to handle the nulls inside your stored procedure?
I am not a fan of Convert.To... I prefer to have "helper" functions (SafeGetInt, SafeGetDecimal or overloaded SafeGetValue) that use the TryParse method of each type - that way I can decide what to do if there is invalid data rather than just having an exception thrown.
Qadeer Ahmed Khan 8-Mar-14 10:07am    
Thanks you are right it would be really painful if I have conversion with in same method when it comes to changing. What about speed? Anything you think can speed up the process? does initializing DataApdapter within every method makes it slow..
CHill60 8-Mar-14 10:19am    
Shouldn't make an appreciable difference unless you are processing thousands of records
Qadeer Ahmed Khan 8-Mar-14 10:25am    
I compared Linq to SQL (on MS SQL Server) and it was quite faster then this not sure what they done. I need better response time as there are not many Record line to fetch but there are rapid requests being made to server.

This is one of the more standard approaches and is ok...
 
Share this answer
 
The speed of fetching data depends on many things, for example:
1) server efficiency (processor, HD and RAM capacity, etc),
2) query performance (one of the most bad practice is to use SELECT * instead SELECT <field_list>),
3) code execution...
etc.

Plese, see these to improve sql query execution time:
MySQL Performance Blog[^]
10 essential performance tips for MySQL[^]
 
Share this answer
 
I'd add a function:
C#
string GetNullableString(DataRow row, string columnName)
{
    return Row[columnName] == DBNull.Value ? null : (string)Row[columnName];
}

for reason of readabilty and better maintenance.
Accessing the columns by number could be a little faster than by name.
But major point is: optimized SQL query with appropriate indices on the table.
I do not use DataAdapters and DataSets, I prefer a simple DataReader - but that might be just a personal preference.

The use of try-catch-throw is a WTF. If you throw from the catch block, throw a "better" exception with the original exception as InnerException.

Those objects for use with Database which implement IDisposable could be wrapped in using blocks - that will make sure they are disposed of properly also in case of an exception. And declare them inside your function, not as class members.
 
Share this answer
 
v2

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