将两个窗体中的数据插入(保存)到数据库winforms sql db中

guicsvcw  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(224)

我正在开发一个小的pos应用程序,用户可以进行支付,并将其保存到数据库中。到目前为止,我存储的数据来自一个表单(datagridview、textboxs等),但现在我决定再添加一个表单(用于处理付款)。其思想是,用户在datagridview(条形码、数量、名称、价格、总计、增值税等**c)中调用数据库中的数据,然后按btnpayment(**打开第二个表单),然后用户给出所需的数据(give payment),然后单击pay按钮后,两个表单的数据应该插入到sql表中现在我想使用相同的存储过程同时将两个表单的数据保存到sqldb中。
在添加第二个表单之前,我使用以下代码插入数据:

try
    {
    conn.Open();

    foreach (DataGridViewRow row in dtgartikuj.Rows)
    {
    if (!row.IsNewRow)
    {
    SqlCommand commandinsert= new SqlCommand("insertfaturimi", conn);
    commandinsert.CommandType = CommandType.StoredProcedure;
    commandinsert.Parameters.Clear();
    commandinsert.Parameters.Add(new SqlParameter("@nr", int.Parse(txtnr.Text)));
    commandinsert.Parameters.Add(new SqlParameter("@client", cmbclient.Text));
    commandinsert.Parameters.Add(new SqlParameter("@subtotal", txtsubtotal.Text));
    commandinsert.Parameters.Add(new SqlParameter("@discount", txtdiscount.Text));
    commandinsert.Parameters.Add(new SqlParameter("@total", txttotal.Text));
    commandinsert.Parameters.Add(new SqlParameter("@vatvalue", txtvatvalue.Text));
    commandinsert.Parameters.Add(new SqlParameter("@productnr", prodnr.Text));
    commandinsert.Parameters.Add(new SqlParameter("@seller", lbluser.Text));
    commandinsert.Parameters.Add(new SqlParameter("@time", DateTime.Now));
    commandinsert.Parameters.Add(new SqlParameter("@barcode", row.Cells[0].Value));
    commandinsert.Parameters.Add(new SqlParameter("@name", row.Cells[1].Value));
    commandinsert.Parameters.Add(new SqlParameter("@qty", row.Cells[2].Value));
    commandinsert.Parameters.Add(new SqlParameter("@vat", row.Cells[4].Value));
    commandinsert.Parameters.Add(new SqlParameter("@price", row.Cells[3].Value));
    commandinsert.Parameters.Add(new SqlParameter("@totalpcs", row.Cells[5].Value));
    commandinsert.Parameters.Add(new SqlParameter("@vatvalueswithoutvatpcs", row.Cells[6].Value));
    commandinsert.Parameters.Add(new SqlParameter("@vatvaluepcs", row.Cells[7].Value));
    commandinsert.ExecuteNonQuery();
    }

    }
    }
    catch (Exception ex)
    {
    MessageBox.Show("Failed" + ex.ToString());
    }
    finally
    {
    conn.Close();
    }

第二个表单有一些文本框(付款和更改)。现在我想把上面的代码放到第二个表单的pay按钮中,但不知道如何将两个表单链接在一起。我的问题是,我应该在上面的代码中做些什么更改,以便能够放入第二个表格插入按钮,然后同时插入两个表格(表格一详细说明了产品)和(第二个表格详细说明了付款情况)
我添加了这个类代码

public class arka_data
    {
        public int NR { get; set; }
        public int BARKODI { get; set; }
        public string EMERTIMI { get; set; }
        public int SASIA {get;set;}
        public float CMIMI {get;set;}
        public float TVSH { get; set; }
        public float TOTAL { get; set; }
        public float NENTOTALI { get; set; }
        public float ZBRITJA { get; set; }
        public float TOTALI { get; set; }
        public DateTime KOHA { get; set; }
        public string KASIERI { get; set; }
        public string KLIENTI { get; set; }
        public float VLERAETVSH { get; set; }
        public float VLERAPATVSH { get; set; }
        public int NRATIKUJVE { get; set; }
        public float TOTALIPCS { get; set; }
        public float VLERATVSHTOTAL { get; set; }

    }
    arka_data dta = new arka_data();
    public void mbushe(string[] args)
    {

       for (int i = 0; i < dataTable.Rows.Count; i++)
      {
          dta.NR = int.Parse(txtnrfatures.Text);
          dta.VLERATVSHTOTAL = float.Parse( textBox1.Text);
          dta.BARKODI = int.Parse(dataTable.Rows[i][0].ToString());
          dta.EMERTIMI = dataTable.Rows[i][1].ToString();
          dta.SASIA = int.Parse(dataTable.Rows[i][2].ToString());
          dta.CMIMI = int.Parse(dataTable.Rows[i][3].ToString());
          dta.TVSH = int.Parse(dataTable.Rows[i][4].ToString());
          dta.NENTOTALI = float.Parse(txttotali.Text);
          dta.ZBRITJA = float.Parse(txtzbritja.Text);
          dta.TOTALI = float.Parse(totali.Text);
          dta.KOHA = DateTime.Now;
          dta.KASIERI = lbluser.Text;
          dta.KLIENTI = cmbklienti.Text;
          dta.VLERAETVSH = float.Parse(dataTable.Rows[i][7].ToString());
          dta.VLERAPATVSH = float.Parse(dataTable.Rows[i][6].ToString());
          dta.NRATIKUJVE = int.Parse(lblnumri.Text);
          dta.TOTALIPCS = float.Parse(dataTable.Rows[i][5].ToString());

但不知道如何调用form2上的方法

beq87vna

beq87vna1#

这里有一个简单的解决方案。我想你需要最少的编码。因此,在企业环境中执行的方式可能会有所不同。
不是从第一个窗体调用存储过程,而是从保存第一个窗体中所有数据元素的数据的类中创建对象。
将该对象传递给第二个窗体。然后用第二种形式创建对象。
为您的数据库访问创建一个单独的类并执行ado.net(类似于您在这里编写的内容)。
使用这两个对象,创建db命令并执行

相关问题