/// <summary> /// Metoda pobierająca z bazy danych MySQL wszystkie rekordy z tabeli i przypisująca je do podanej tabeli (tableProducts). /// </summary> /// <param name="dbClient"></param> /// <param name="tableProducts"></param> /// <returns></returns> public static bool GetAll(Database.Client dbClient, List <Database.Structure.Tables.Products> tableProducts) { if (dbClient == null || dbClient.Connection() == null) { return(false); } if (!dbClient.OpenConnection()) // próba nawiązania połączenia z bazą { return(false); } try { MySqlDataAdapter mySqlDa = new MySqlDataAdapter("ProductsGetAll", dbClient.Connection()); mySqlDa.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure; System.Data.DataTable dtProducts = new System.Data.DataTable(); mySqlDa.Fill(dtProducts); tableProducts.Clear(); for (int i = 0; i < dtProducts.Rows.Count; i++) { Database.Structure.Tables.Products tableItem = new Database.Structure.Tables.Products(); int j = 0; tableItem.Idproduct = (int)dtProducts.Rows[i].ItemArray[j++]; tableItem.Idtype = (int)dtProducts.Rows[i].ItemArray[j++]; tableItem.Idplatform = (int)dtProducts.Rows[i].ItemArray[j++]; tableItem.Name = dtProducts.Rows[i].ItemArray[j++].ToString(); tableItem.Subname = dtProducts.Rows[i].ItemArray[j++].ToString(); tableItem.Buy_price = OperationHelper.PrepareInt32Value(dtProducts.Rows[i].ItemArray[j++].ToString()); tableItem.Sell_price = OperationHelper.PrepareInt32Value(dtProducts.Rows[i].ItemArray[j++].ToString()); tableItem.Rent_price = OperationHelper.PrepareInt32Value(dtProducts.Rows[i].ItemArray[j++].ToString()); tableItem.Date_added = OperationHelper.PrepareDateTimeValue(dtProducts.Rows[i].ItemArray[j++].ToString()); tableItem.Last_update = OperationHelper.PrepareDateTimeValue(dtProducts.Rows[i].ItemArray[j].ToString()); tableProducts.Add(tableItem); } return(true); } catch (MySqlException ex) { Trace.WriteLine(ex.Message); return(false); } // pamiętać, aby zamykać połączenie }
/// <summary> /// Metoda pobierająca z bazy danych MySQL pojedynczy rekord o podanym identyfikatorze (IdProduct) i przypisująca go do podanej strukturze tabeli (tableProductsItem). /// </summary> /// <param name="dbClient"></param> /// <param name="IdProduct"></param> /// <param name="tableProductsItem"></param> /// <returns></returns> public static bool Get(Database.Client dbClient, int IdProduct, Database.Structure.Tables.Products tableProductsItem) { if (dbClient == null || dbClient.Connection() == null) { return(false); } if (!dbClient.OpenConnection()) // próba nawiązania połączenia z bazą { return(false); } try { MySqlCommand mySqlCmd = new MySqlCommand("ProductsGet", dbClient.Connection()); mySqlCmd.CommandType = System.Data.CommandType.StoredProcedure; mySqlCmd.Parameters.AddWithValue("_IdProduct", IdProduct); mySqlCmd.ExecuteNonQuery(); MySqlDataReader reader = mySqlCmd.ExecuteReader(); if (reader.Read()) { int i = 0; tableProductsItem.Idproduct = (int)reader[i++]; tableProductsItem.Idtype = (int)reader[i++]; tableProductsItem.Idplatform = (int)reader[i++]; tableProductsItem.Name = reader[i++].ToString(); tableProductsItem.Subname = reader[i++].ToString(); tableProductsItem.Buy_price = OperationHelper.PrepareInt32Value(reader[i++].ToString()); tableProductsItem.Sell_price = OperationHelper.PrepareInt32Value(reader[i++].ToString()); tableProductsItem.Rent_price = OperationHelper.PrepareInt32Value(reader[i++].ToString()); tableProductsItem.Date_added = OperationHelper.PrepareDateTimeValue(reader[i++].ToString()); tableProductsItem.Last_update = OperationHelper.PrepareDateTimeValue(reader[i].ToString()); reader.Close(); } return(true); } catch (MySqlException ex) { Trace.WriteLine(ex.Message); return(false); } // pamiętać, aby zamykać połączenie }