public ObservableCollection <Furniture> GetAll() { ObservableCollection <Furniture> furniture = new ObservableCollection <Furniture>(); string commandText = @"SELECT * FROM Furniture WHERE Deleted = 0"; //Treba mi SqlConnection, SqlCommand i DataReader using (con = new SqlConnection(ConfigurationManager.ConnectionStrings["FurnitureStore"].ConnectionString)) { con.Open(); SqlCommand command = con.CreateCommand(); command.CommandText = commandText; SqlDataReader dataReader = command.ExecuteReader(); if (dataReader.HasRows) { while (dataReader.Read()) { int id = (int)dataReader["Id"]; string name = (string)dataReader["Name"]; int quantity = (int)(Int16)dataReader["Quantity"]; decimal price = (decimal)dataReader["Price"]; int furnitureTypeId = (int)dataReader["FurnitureTypeId"]; FurnitureType furnitureType = new FurnitureTypeDAO().Get(furnitureTypeId); int actionSaleId; try { actionSaleId = (int)dataReader["ActionSaleId"]; } catch (Exception exc) { Console.WriteLine(exc.Message); actionSaleId = -1; } ActionSale actionSale = actionSaleId == -1 ? null : new ActionSaleDAO().Get(actionSaleId); bool deleted = (bool)dataReader["Deleted"]; furniture.Add(new Furniture() { Id = id, Name = name, Quantity = quantity, Price = price, FurnitureType = furnitureType, ActionSale = actionSale, Deleted = deleted }); } } dataReader.Close(); } return(furniture); }
public Furniture Get(int furnitureId) { string commandText = @"SELECT * FROM Furniture WHERE Id = @Id"; using (con = new SqlConnection(ConfigurationManager.ConnectionStrings["FurnitureStore"].ConnectionString)) { con.Open(); SqlCommand command = con.CreateCommand(); command.CommandText = commandText; command.Parameters.Add(new SqlParameter("@Id", furnitureId)); using (SqlDataReader dataReader = command.ExecuteReader()) { if (dataReader.Read()) { int id = (int)dataReader["Id"]; string name = (string)dataReader["Name"]; int quantity = (int)(Int16)dataReader["Quantity"]; decimal price = (decimal)dataReader["Price"]; int furnitureTypeId = (int)dataReader["FurnitureTypeId"]; FurnitureType furnitureType = new FurnitureTypeDAO().Get(furnitureTypeId); int actionSaleId; try { actionSaleId = (int)dataReader["ActionSaleId"]; } catch (Exception exc) { Console.WriteLine(exc.Message); actionSaleId = -1; } ActionSale actionSale = actionSaleId == -1 ? null : new ActionSaleDAO().Get(actionSaleId); bool deleted = (bool)dataReader["Deleted"]; return(new Furniture() { Id = id, Name = name, Quantity = quantity, Price = price, FurnitureType = furnitureType, ActionSale = actionSale, Deleted = deleted }); } } return(null); } }