public (List<Order>, Dictionary<int, string>, Dictionary<int, string>) GetOrders(int RestaurantID, int StartID)
{
List<Order> Orders = null;
Dictionary<int, string> MenuItemsXml = null;
Dictionary<int, string> OrderHistoryXml = null;
string sql = "SELECT ev.ID, ev.RestaurantID, ev.UserID, ev.DateCreated, ev.Amount, ev.AmountDiscounted, ev.TableNumber, (SELECT '|', FoodOrderDetail.ItemID, FoodOrderDetail.ItemName, FoodOrderDetail.Quantity, FoodOrderDetail.[Price], FoodOrderDetail.[Note] FROM FoodOrderDetail INNER JOIN FoodOrders a ON FoodOrderDetail.OrderID = a.ID WHERE a.ID = ev.ID ORDER BY DateCreated DESC FOR XML PATH('')) [OrderItems],(SELECT '|', FoodOrderHistory.[Status], FoodOrderHistory.[StatusDate] FROM FoodOrderHistory INNER JOIN FoodOrders a ON FoodOrderHistory.OrderID = a.ID WHERE a.ID = ev.ID ORDER BY StatusDate ASC FOR XML PATH('')) [OrderHistory] FROM FoodOrders ev WHERE ev.RestaurantID = @param1 AND ev.ID > @param2 AND ev.PreAuthorised = 1 ORDER BY ev.ID DESC;";
using (var connection = new SqlConnection(ConnectionString))
{
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.Add(new SqlParameter("@param1", RestaurantID));
command.Parameters.Add(new SqlParameter("@param2", StartID));
connection.Open();
using SqlDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
if (dataReader.HasRows)
{
Orders = new List<Order> { };
while (dataReader.Read())
{
Order objOrder = new Order();
objOrder.ID = Convert.ToInt32(dataReader["ID"]);
objOrder.UserID = Convert.ToInt32(dataReader["UserID"]);
objOrder.DateCreated = Convert.ToDateTime(dataReader["DateCreated"]);
objOrder.Amount = Convert.ToDecimal(dataReader["Amount"]);
if (dataReader["AmountDiscounted"] != DBNull.Value)
{
objOrder.Amount = Convert.ToDecimal(dataReader["AmountDiscounted"]);
}
if (dataReader["TableNumber"] != DBNull.Value)
{
objOrder.TableNumber = Convert.ToString(dataReader["TableNumber"]);
}
if (dataReader["OrderItems"] != DBNull.Value)
{
if(MenuItemsXml == null)
{
MenuItemsXml = new Dictionary<int, string>();
}
MenuItemsXml.Add(objOrder.ID, Convert.ToString(dataReader["OrderItems"]));
}
if(dataReader["OrderHistory"] != DBNull.Value)
{
if(OrderHistoryXml == null)
{
OrderHistoryXml = new Dictionary<int, string>();
}
OrderHistoryXml.Add(objOrder.ID, Convert.ToString(dataReader["OrderHistory"]));
}
Orders.Add(objOrder);
}
}
dataReader.Close();
}
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
return (Orders, MenuItemsXml, OrderHistoryXml);
}
Вышеупомянутая функция вызывается этой функцией:
public List<Order> GetOrders(int RestaurantID, int StartID)
{
List<Order> Orders = null;
Dictionary<int, string> OrderItemsXml = null;
Dictionary<int, string> OrderHistoryXml = null;
OrderData objData = new OrderData();
try
{
(Orders, OrderItemsXml, OrderHistoryXml) = objData.GetOrders(RestaurantID, StartID);
}
catch (Exception ex)
{
logger.Error(ex);
}
if(OrderItemsXml != null)
{
foreach (var order in Orders)
{
try
{
// stripe makes us store everything in major currency unit e.g. £1 is 100, so divide by 100 for when we later need to display values
if(order.Amount > 0)
{
order.Amount /= 100;
}
if (OrderItemsXml.ContainsKey(order.ID))
{
order.OrderItems = ConvertXmlToItemsList(OrderItemsXml[order.ID].TrimStart("|".ToCharArray()));
}
else
{
logger.Warn(string.Format("Dictionary OrderItemsXml does not contain OrderId Key of {0} in OrderManager", order.ID.ToString()));
}
if (OrderHistoryXml.ContainsKey(order.ID))
{
order.OrderHistory = ConvertXmlOrderHistory(OrderHistoryXml[order.ID].TrimStart("|".ToCharArray()));
}
else
{
logger.Warn(string.Format("Dictionary OrderHistoryXml does not contain OrderId Key of {0} in OrderManager", order.ID.ToString()));
}
}
catch (Exception ex)
{
logger.Error(ex);
}
}
}
else
{
logger.Warn(string.Format("No order items found for RestaurantID {0} and StartID {1} in method GetOrders() in OrderManager", RestaurantID, StartID));
}
return Orders;
}
Функция GetOrders () всегда закрывает соединение с базой данных? Иногда я получаю ошибки тайм-аута, и наиболее вероятная причина — в пуле соединений не хватает соединений. Когда это происходит, загрузка ЦП достигает 100%. Это происходит примерно раз в день.