public void RemoveMedicine() { const string sqlQuery = "DELETE FROM Medicines where Id=@Id"; try { using (var connection = new SqlConnection(ActiveRecord.ConnectionString)) using (var command = new SqlCommand(sqlQuery, connection)) { var pId = new SqlParameter("@Id", SqlDbType.Int) { Value = Id }; command.Parameters.Add(pId); connection.Open(); var result = command.ExecuteNonQuery(); if (result == 1) { Medicine.ShowMedicines(); Console.WriteLine($"Record {Name} deleted successfully."); Thread.Sleep(1000); } else { Console.WriteLine("No records deleted"); Thread.Sleep(1000); } } } catch (Exception e) { Console.WriteLine(e.Message); } }
public static void AddEM() { Console.Write("Podaj nazwę leku: "); string name = Console.ReadLine().Trim(); Console.Write("Podaj nazwę producenta: "); string manufacturer = Console.ReadLine().Trim(); Console.Write("Podaj cenę leku: "); decimal price = Decimal.Parse(Console.ReadLine().Trim()); Console.Write("Podaj ilość: "); string amountstr = Console.ReadLine().Trim(); int amount = Int32.Parse(amountstr); Console.Write("Czy jest to lek na recepte [T/N]: "); string withPrescriptionstr = Console.ReadLine().Trim().ToLower(); bool withPrescription; if (withPrescriptionstr == "t") { withPrescription = true; } else if (withPrescriptionstr == "n") { withPrescription = false; } else { withPrescription = false; Console.WriteLine("Podano zła Komende. Przyjęto N"); } Medicine medicine = new Medicine(name, manufacturer, price, amount, withPrescription); try { using (SqlConnection connection = new SqlConnection(connectionString)) { var sqlCommand = new SqlCommand(); sqlCommand.Connection = connection; sqlCommand.CommandText = @"INSERT INTO Mediciness (Name, Manufacturer, Price, Amount, WithPrescription) VALUES (@Name, @Manufacturer, @Price, @Amount, @WithPrescription);" ; var sqlNameParam = new SqlParameter { DbType = System.Data.DbType.AnsiString, Value = medicine.Name, ParameterName = "@Name" }; var sqlManufacturerParam = new SqlParameter { DbType = System.Data.DbType.AnsiString, Value = medicine.Manufacturer, ParameterName = "@Manufacturer" }; var sqlPriceParam = new SqlParameter { DbType = System.Data.DbType.Decimal, Value = medicine.Price, ParameterName = "@Price" }; var sqlAmountParam = new SqlParameter { DbType = System.Data.DbType.Int32, Value = medicine.Amount, ParameterName = "@Amount" }; var sqlWithPrescriptionParam = new SqlParameter { DbType = System.Data.DbType.Boolean, Value = medicine.WithPrescription, ParameterName = "@WithPrescription" }; sqlCommand.Parameters.Add(sqlNameParam); sqlCommand.Parameters.Add(sqlManufacturerParam); sqlCommand.Parameters.Add(sqlPriceParam); sqlCommand.Parameters.Add(sqlAmountParam); sqlCommand.Parameters.Add(sqlWithPrescriptionParam); connection.Open(); sqlCommand.ExecuteNonQuery(); connection.Close(); } } catch (Exception e) { Console.WriteLine(e.Message); } }