c# - 将两个表单中的数据插入(保存)到数据库中 winforms sql db
问题描述
我正在开发一个小型 pos 应用程序,用户可以在其中付款并将其保存到数据库中。到目前为止,我从一个表单(datagridview、文本框等)存储数据,但现在我决定再添加一个表单(用于处理付款)。思路是用户在datagridview( *barcode, qty, name, price, total, vat et***c)中调用db中的数据,然后按btnpayment(***opens the 2nd form* ),然后用户提供所需的数据(支付),然后单击支付按钮后,两个表单中的数据应该插入到 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上的方法
解决方案
这是一个简单的解决方案。我假设您需要最少的编码。因此,在企业环境中完成的方式可能会有所不同。
不是从第一个表单调用存储过程,而是从一个类中创建一个对象,该类保存第一个表单中所有数据元素的数据。
将该对象传递给第二种形式。然后用第二种形式创建对象。
为您的数据库访问创建一个单独的类并执行 ADO.Net(类似于您在此处编写的内容)。
使用 2 个对象,创建 DB 命令并执行