Insert List to table

后端 未结 2 415
礼貌的吻别
礼貌的吻别 2021-01-22 13:47

I had this method to insert new company to database but I have problem with List because I want to be able to insert multiple contact for company. C

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-22 14:18

    Your stored procedure is inserting only one contact for the company. If you would like to add another contact (or more of them), write another procedure that will insert new contacts using the CompanyID from the last company inserted to the database.

    I can't write you code because I don't know the tables and can't replicate the results, but you should create another procedure from this code

    INSERT INTO [TaskManagementSystem_DB].[dbo].[Company_Contacts]
               ([companyID] 
    
               ,[labelContactTypeID]
               ,[contactDetails]
               ,[status]
               ,[notes])
         VALUES
               (@CompanyID 
               ,@LabelContactTypeID
               ,@ContactDetails
               ,@Status
               ,@Notes)
    

    In the first part just add one contact from the list (the first one that is on index 0 in the list)

    Read the CompanyID of company that has been just inserted and for each other contact in the list, using this ID call your new procedure. It will add new contacts that are referenced to the company using the same companyID (referential identity one to many).

    EDIT You should do something like this:

    public static bool AddNewCompany(Company company,List contact , Location local)
    {
        // get a configured DbCommand object
        DbCommand comm = GenericDataAccess.CreateCommand();
    //Set the store Proc name 
    comm.CommandText = "AddNewCompany";
    
    
    //create new parameter @CompanyName
    DbParameter param = comm.CreateParameter();
    param.ParameterName = "@CompanyName";
    param.Value = company.CompanyName;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @CompanyDetail 
    param = comm.CreateParameter();
    param.ParameterName = "@CompanyDetail";
    param.Value = company.CompanyDetail;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @ModifiedDate
    param = comm.CreateParameter();
    param.ParameterName = "@ModifiedDate";
    param.Value = DateTime.Now;
    param.DbType = DbType.DateTime;
    comm.Parameters.Add(param);
    
        //Company Info
    //add only one contact
        /create new parameter @LabelContactTypeID
            param = comm.CreateParameter();
            param.ParameterName = "@LabelContactTypeID";
            param.Value = contact[0].LabelContactTypeID;
            param.DbType = DbType.StringFixedLength;
            comm.Parameters.Add(param);
    
        //create new parameter @ContactDetails
        param = comm.CreateParameter();
        param.ParameterName = "@ContactDetails";
        param.Value = contact[0].ContactDetail;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);
    
        //create new parameter @Status
        param = comm.CreateParameter();
        param.ParameterName = "@Status";
        param.Value = contact[0].Status;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);
    
        //create new parameter @Notes
        param = comm.CreateParameter();
        param.ParameterName = "@Notes";
        param.Value = contact[0].Notes;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);
    
    
    //Company Info
    //create new parameter @Address
    param = comm.CreateParameter();
    param.ParameterName = "@Address";
    param.Value = local.Address;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @City
    param = comm.CreateParameter();
    param.ParameterName = "@City";
    param.Value = local.City;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @Province
    param = comm.CreateParameter();
    param.ParameterName = "@Province";
    param.Value = local.Province;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @PostalCode
    param = comm.CreateParameter();
    param.ParameterName = "@PostalCode";
    param.Value = local.PostalCode;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @Note
    param = comm.CreateParameter();
    param.ParameterName = "@Note";
    param.Value = local.Note;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @ModifiedDateLocation  
    param = comm.CreateParameter();
    param.ParameterName = "@ModifiedDateLocation";
    param.Value = DateTime.Now;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    
    try
    {
        return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
    
    }
    catch
    {
        return false;
    }
    

    }

    Create a new method that adds new contact using the part of the procedure I pasted here and insert all other contacts. Pas to this method a companyID.

提交回复
热议问题