0

我正在尝试插入两个表,因为一个是客户的主要详细信息,另一个表是客户的子详细信息。意味着第一行将从控件获取参数,第二个表将从 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 行一样触发。我将参数传递为

 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
        }

cstCls.CstmrInsUp(txtCustmr.Text, txtShrtNam.Text, txtLn1.Text, txtLn2.Text, txtCity.Text,pin.ToString(),txtSpLn1.Text,txtSpLn2.Text,txtSpCty.Text,pin.ToString(), Phn1. ToString(),Phn2.ToString(),fax.ToString(),txtEmail.Text,txtTinNo.Text,cstnam,dsgntn,mblNo,eMail);

通过做这个。第一个记录只是保存。我如何实现从 datagridview 和控件中插入所有行。谢谢

4

1 回答 1

0

在您的 MethodCstmrInsUp变量cstId中始终为1. 我认为这是主键值,因此它使用 value 插入第一条记录1。然后之后它不能插入另一行相同custid的。所以它抛出一个错误并且事务被回滚。

于 2013-01-21T08:06:23.823 回答