简体   繁体   English

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

[英]Inserting into two tables as single row and Multiple rows in c#

I am trying to insert into two tables as one is main details of customer and another table is sub details of customer. 我试图插入到两个表中,因为一个表是客户的主要详细信息,而另一个表是客户的子详细信息。 Means 1st row will take parameters from controls and 2nd table will take from datagridview. 意味着第一行将从控件中获取参数,第二行将从datagridview中获取。 Those two has to done in one method. 这两个必须用一种方法完成。 I tried as 我尝试了

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();
        }

    }

Here customersdtls table should fire as in datagridview rows. 在这里,customersdtls表应该像在datagridview行中一样触发。 Iam passing parameters as 我将参数传递为

 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); 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);

By doing this. 通过做这个。 First one record is only saving. 第一个记录只是保存。 How can i implement to insert all rows from datagridview and controls. 我如何实现从datagridview和控件插入所有行。 Thank you 谢谢

In your Method CstmrInsUp variable cstId is always 1 . 在您的方法CstmrInsUp变量cstId始终为1 I think this is the primary key value, so it is inserting the first record with value 1 . 我认为这是主键值,因此它将插入值为1的第一条记录。 Then after that it cannot insert another row with same custid . 然后,它不能再插入另一个具有相同的custid行。 So it is throwing an error and the transaction is rolled back. 因此,它将引发错误,并且事务将回滚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM