0

I have a scenario where i need to perform INSERT Operation on multiple SQL tables, There is a dependency for inserting records in each table.

I want to perform first INSERT to Deal table and then list of contacts and companies to the respective tables, similarly for associated contacts and companies INSERT.

All INSERT operations should be part of one SQL Transaction so that there should be no orphan record entry in the database tables.

I am thinking to put all INSERT operation logic in a single Stored Procedure but don't have enough understanding of same for processing the list of records(in case of contacts and companies) from C# to SQL.

Below is the Insert Table dependency criteria -

The main table => Deal ()

  • INSERT Deal (get INSERTED dealid to INSERT it in associated contacts and companies table)
  • INSERT list of contact in Contact table (get INSERTED contactid to INSERT it in associated contact table)
  • INSERT list of associated contact in DealContact table (dealid, contactid)

  • INSERT list of company in Company table(get INSERTED companyid to INSERT it in associated company table)

  • INSERT list of associated companies in DealCompany table (dealid, companyid)

Question: How to perform below SQL operations in single SQL transaction, to make sure there are no orphan record entries in the respective tables?

  • INSERT List of contacts and then List of Associated Contacts for each deal
  • INSERT List of companies and then List of Associated companies for each deal

Below is the C# method where I am trying to put this Insert operations -

public void ProcessDeals(List<Result> lstDeals)
{
    if (_objReq == null)
        _objReq = new RequestHandler();
    string strBU = string.Empty;
    foreach (Result deal in lstDeals)
    {
        #region Associated Contacts
        Task<AssociatedContacts> taskAscContacts = _objReq.Get<AssociatedContacts>(_limit, deal.id);
        //store response in a list of associated contacts
        List<AscContact> ascContacts = taskAscContacts.Result.results;      

        //pull cotact information from API using contact id
        List<Contact> lstContacts = new List<Contact>();
        foreach (AscContact contact in ascContacts)
        {
            Task<Contact> taskContact = _objReq.Get<Contact>(100, contact.id);
            Contact contactinfo = taskContact.Result;

            //store contact in list of contacts
            lstContacts.Add(contactinfo);   
        }
        #endregion

        #region Associated Companies
        Task<AssociatedCompanies> taskAscCompanies = _objReq.Get<AssociatedCompanies>(_limit, deal.id);
        //store response in a list of associated companies
        List<AscCompany> ascCompanies = taskAscCompanies.Result.results;

        //pull cotact information
        List<Company> lstCompanies = new List<Company>();
        foreach (AscCompany company in ascCompanies)
        {
            Task<Company> taskCompany = _objReq.Get<Company>(100, company.id);

            //store contact in list of contacts
            lstCompanies.Add(taskCompany.Result);
        }
        #endregion

        //TODO: finally db insert operations

        //Step 1 - Insert into deal
        //Step 2 - Insert into contacts
        //Step 3 - Insert into companies

        //Step 4 - Insert into Ass Contact 

        //Step 5 -- Insert into Ass company
    }
}

Appreciate any suggestions.

Rahul Hendawe
  • 902
  • 1
  • 14
  • 39

0 Answers0