【问题标题】:Inserting(saving) data from two forms into database winforms sql db将两个表单中的数据插入(保存)到数据库中 winforms sql db
【发布时间】:2020-05-24 16:46:43
【问题描述】:

我正在开发一个小型 pos 应用程序,用户可以在其中付款并将其保存到数据库中。到目前为止,我从一个表单(datagridview、文本框等)存储数据,但现在我决定再添加一个表单(用于处理付款)。想法是用户在 datagridview(*barcode, qty, name, price, total, vat et***c) 中调用 db 中的数据,然后按 btnpayment(***opens the 2nd form*),然后用户提供所需的数据(give payment),然后点击支付按钮后,两个表单的数据应该插入到sql表中 现在我想使用相同的存储过程将两个表单中的数据同时保存到 sql db 中。

在添加第二个表单之前,我使用此代码插入数据:

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

第二种形式有一些文本框(付款和找零)。现在我想将上面的代码放入第二个表单支付按钮,但不知道如何将两个表单链接在一起。我的问题是我应该在上面的代码中更改什么以便能够放入第二个表单插入按钮,然后同时插入两个表单(表单一个包含产品详细信息)和(第二个表单包含付款详细信息)

我添加了这个类代码

        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上的方法

【问题讨论】:

  • 您可以将数据添加到第二个表单中,在表单中,设置这些数据,然后在第二个表单关闭时,从第一个表单中引用数据并保存。

标签: c# sql winforms insert


【解决方案1】:

这是一个简单的解决方案。我假设您需要最少的编码。因此,在企业环境中完成的方式可能会有所不同。

不是从第一个表单调用存储过程,而是从一个类中创建一个对象,该类保存第一个表单中所有数据元素的数据。

将该对象传递给第二种形式。 然后用第二种形式创建对象。

为您的数据库访问创建一个单独的类并执行 ADO.Net(类似于您在此处编写的内容)。

使用 2 个对象,创建 DB 命令并执行

【讨论】:

  • @i 创建了该类以及该类中的对象,但现在我无法调用该方法。 PS我更新了问题,我添加了类的代码部分。
  • 如果您要学习,请缩小班级规模并首先尝试理解这个概念。然后将其应用于冗长的代码。否则,很难通过冗长的代码。
  • 在您的情况下,您可以在两个表单之外创建类并作为公共访问。然后在 Form2 中有一个属于该类类型的属性。检查这个:stackoverflow.com/questions/40210717/…
猜你喜欢
  • 2013-10-05
  • 2016-09-02
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多