gpt4 book ai didi

mysql - 如何从 ListView 导出数据并保存在数据库中

转载 作者:行者123 更新时间:2023-11-29 13:19:56 26 4
gpt4 key购买 nike

我正在开发库存软件,其中我已将数据保存在 ListView 中,现在我想将数据保存在 MY SQL 的 ListView 中,其中我的表的字段与 ListView 中的字段相同,我希望每当我单击在“完成”按钮上,数据只需保存到 ListView 中的表中,在表中添加数据的代码是:

 {    
int totalPrice = 0;
int val1, val2;
string totalp;

val1 = Convert.ToInt32(txtProductPrice.Text);
val2 = Convert.ToInt32(txtProductQuantity.Text);
totalPrice = val1 * val2;

totalp = Convert.ToString(totalPrice);

// int totalPrice = 0;

lvProductInfo.Items.Add(""); // QuestionID
lvProductInfo.Items[lvProductInfo.Items.Count - 1].SubItems.Add(txtProdcutCode.Text); //Question
lvProductInfo.Items[lvProductInfo.Items.Count - 1].SubItems.Add(txtProductQuantity.Text); //Option1
lvProductInfo.Items[lvProductInfo.Items.Count - 1].SubItems.Add(txtProductPrice.Text); //Option2
lvProductInfo.Items[lvProductInfo.Items.Count - 1].SubItems.Add(totalp); //Option3

//clearing text boxes.
txtProdcutCode.Clear();
txtProductQuantity.Clear();
txtProductPrice.Clear();
txtCashRecived.Clear();

//focusing on product code.
txtProdcutCode.Focus();
}

现在我很困惑如何从 ListView 访问数据并将其保存在 SQL 表中的连续字段中,表中的字段名称为 ItemNo、ProductCode、ProductQuantity、ProductPrice。

编辑:

我已尝试使用以下代码将数据添加到数据库表中,我可以添加除字符串类型的产品代码之外的所有内容(例如productPrice、ProductQuantity)。我尝试过的代码是:

  private void button1_Click(object sender, EventArgs e)
{
string sql;

int ProductQuantity = 0;
string ProductCode;
int ProductPrice = 0;
int totalPrice = 0;

for (int i = 0; i < lvProductInfo.Items.Count; i++)
{
ProductCode += Convert.ToInt32(lvProductInfo.Items[i].SubItems[1].Text);
ProductQuantity += Convert.ToInt32(lvProductInfo.Items[i].SubItems[2].Text);
ProductPrice += Convert.ToInt32(lvProductInfo.Items[i].SubItems[3].Text);
totalPrice += Convert.ToInt32(lvProductInfo.Items[i].SubItems[4].Text);
}

sql = "";
sql += "INSERT INTO PurchaseLog (ProductCode,ProductQuantity,ProductPrice,TotalPrice)";
sql += " VALUES ('" + ProductCode + "','" + ProductQuantity + "','" + ProductPrice + "','" + totalPrice + "')";

clsConnection clsCn = new clsConnection();
SqlConnection cn = null;
SqlCommand cmd = new SqlCommand();

clsCn.fnc_ConnectToDB(ref cn);

cmd.Connection = cn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

this.Close();
}

但我无法将产品代码添加到数据库表中。请引导我完成它。

最佳答案

真有趣!您需要扩展 for 循环并停止将值连接到变量上。如果运行调试器并查看生成的 sql 字符串,您将看到发生了什么。

代码应该是这样的:

    for (int i = 0; i < lvProductInfo.Items.Count; i++)
{
ProductCode = Convert.ToInt32(lvProductInfo.Items[i].SubItems[1].Text);
ProductQuantity = Convert.ToInt32(lvProductInfo.Items[i].SubItems[2].Text);
ProductPrice = Convert.ToInt32(lvProductInfo.Items[i].SubItems[3].Text);
totalPrice = Convert.ToInt32(lvProductInfo.Items[i].SubItems[4].Text);


sql = "";
sql = "INSERT INTO PurchaseLog (ProductCode,ProductQuantity,ProductPrice,TotalPrice)"
+ " VALUES ('" + ProductCode + "','" + ProductQuantity + "','" + ProductPrice + "','" + totalPrice + "')";

clsConnection clsCn = new clsConnection();
SqlConnection cn = null;
SqlCommand cmd = new SqlCommand();

clsCn.fnc_ConnectToDB(ref cn);

cmd.Connection = cn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();


this.Close();
}

}

注意不再有“+=”,您将一一添加ListViewItems。

关于mysql - 如何从 ListView 导出数据并保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20989691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com