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

时间:2013-01-21 07:31:06

标签: 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 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和控件插入所有行。谢谢

1 个答案:

答案 0 :(得分:0)

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