using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Transactions; namespace BealeEngineering.Core.Data.Database.Client { public class CreatePurchaseOrderAllocation : Connection { public CreatePurchaseOrderAllocation(string sqlConnectionString) : base(sqlConnectionString) { } public int ByDictionary(Dictionary saleInvoiceIdToPoLineId) { int rowsCreated = 0; if (saleInvoiceIdToPoLineId == null || !saleInvoiceIdToPoLineId.Any()) { throw new Exception("No data passed to function."); } using (TransactionScope scope = new TransactionScope()) { using (SqlConnection conn = new SqlConnection(sqlConnectionString)) { conn.Open(); foreach (var item in saleInvoiceIdToPoLineId) { using (SqlCommand cmd = new SqlCommand(@" INSERT INTO ClientPurchaseOrderLineSalesInvoice ( ClientPurchaseOrderLineID ,SaleInvoiceID ) VALUES ( @clientPurchaseOrderLineID ,@saleInvoiceID ) ", conn)) { cmd.Parameters.AddWithValue("@clientPurchaseOrderLineID", item.Value); cmd.Parameters.AddWithValue("@saleInvoiceID", item.Key); rowsCreated = rowsCreated + cmd.ExecuteNonQuery(); } } } scope.Complete(); } return rowsCreated; } } }