C# 在c中作为单行和多行插入到两个表中#

C# 在c中作为单行和多行插入到两个表中#,c#,datagridview,insert-update,C#,Datagridview,Insert Update,我试图插入到两个表中,因为一个表是客户的主要详细信息,另一个表是客户的子详细信息。表示第一行将从控件获取参数,第二个表将从datagridview获取参数。这两个问题必须用一种方法解决。我试着 public void CstmrInsUp(string cstName, string cstSName, string AdLn1, string AdLn2, string AdCity, string AdPin, string SAdLn1, string SAdLn2, string SAd

我试图插入到两个表中,因为一个表是客户的主要详细信息,另一个表是客户的子详细信息。表示第一行将从控件获取参数,第二个表将从datagridview获取参数。这两个问题必须用一种方法解决。我试着

public void CstmrInsUp(string cstName, string cstSName, string AdLn1, string AdLn2, string AdCity, string AdPin, string SAdLn1, string SAdLn2, string SAdCity, string SAdPin, string TelPh1, string TelPh2, string FaxNo, string MailId, string TinNo,string cstDtlName,string cstDsgntn,string cstDtMobl,string cstDtEmail)
    {
       try
        {
            int cstId=1;
            int? grntdId=0;

            cstmrDC.Connection.Open();
            trns = cstmrDC.Connection.BeginTransaction();
            cstmrDC.Transaction = trns;

            cstmrDC.customers_iu(cstId, cstName, cstSName, AdLn1, AdLn2, null, AdCity, AdPin, SAdLn1, SAdLn2, null, SAdCity, SAdPin, TelPh1, TelPh2, FaxNo, MailId, null, TinNo, 1,ref grntdId);
            cstmrDC.customerscntcts_iu(null, cstId, cstDtlName, cstDsgntn, cstDtMobl, cstDtEmail, 1);
            trns.Commit();



        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            if (trns != null)
                trns.Rollback();
        }

    }
在这里,customersdtls表应该像datagridview行一样启动。Iam将参数传递为

 for (int i = 0; i < dgvCustInfo.Rows.Count - 1; i++)
        {
            cstnam = dgvCustInfo.Rows[i].Cells[0].Value.ToString();
            dsgntn = dgvCustInfo.Rows[i].Cells[1].Value.ToString();
            mblNo = dgvCustInfo.Rows[i].Cells[3].Value.ToString();
            eMail = dgvCustInfo.Rows[i].Cells[4].Value.ToString();
            //cst.InsrtDgv(cstnam, dsgntn, extNo, mblNo, eMail);
            //write a log event with the user information , system information, and activity
        }
for(int i=0;i
cstCls.cstmrisup(txtCustmr.Text,txtShrtNam.Text,txtLn1.Text,txtLn2.Text,pin.ToString(),txtSpLn1.Text,txtSpLn2.Text,txtSpCty.Text,pin.ToString(),Phn1.ToString(),Phn2.ToString(),fax.ToString(),txtmail.Text,txttino.Text,cstnam,dsgntn,mblNo,eMail)


通过这样做。第一条记录仅用于保存。如何实现从datagridview和控件插入所有行。谢谢你

在你的方法中
CstmrInsUp
变量
cstId
总是
1
。我认为这是主键值,因此它插入了值为
1
的第一条记录。然后,它不能插入另一行具有相同的
custid
。因此它抛出了一个错误,事务被回滚。

您是否尝试了
@@identity
?从一个
客户
身份到另一个表传输?