public void AddBatch(Batch batch, BatchContent batchContent) { try { string batchCommandString = @"INSERT INTO Batch (OperationType, Date) VALUES (@OperationType, @Date)"; var batchCommand = new SqlCommand(batchCommandString, _con); batchCommand.Parameters.AddWithValue("@OperationType", batch.OperationType); batchCommand.Parameters.AddWithValue("@Date", batch.Date); _con.Open(); batchCommand.ExecuteNonQuery(); string batchContentCommandString = @"INSERT INTO BatchContent (GoodsId, BatchId, Quantity) VALUES (@GoodsId, @BatchId, @Quantity)"; var batchContentCommand = new SqlCommand(batchContentCommandString, _con); batchContentCommand.Parameters.AddWithValue("@GoodsId", batchContent.GoodsId); batchContentCommand.Parameters.AddWithValue("@BatchId", batchContent.BatchId); batchContentCommand.Parameters.AddWithValue("@Quantity", batchContent.Quantity); batchContentCommand.ExecuteNonQuery(); _con.Close(); } catch (Exception) { _con.Close(); throw; } }
static void AddBatch(Repository repo) { var batch = new Batch(); Console.WriteLine("Enter all values:"); Console.WriteLine("Operation type:"); batch.OperationType = Console.ReadLine(); Console.WriteLine("Date:"); batch.Date = DateTime.Parse(Console.ReadLine()); var batchContent = new BatchContent(); Console.WriteLine("GoodsId:"); batchContent.GoodsId = int.Parse(Console.ReadLine()); Console.WriteLine("BatchId:"); batchContent.BatchId = int.Parse(Console.ReadLine()); Console.WriteLine("Quanity:"); batchContent.Quantity = int.Parse(Console.ReadLine()); repo.AddBatch(batch, batchContent); }