如何将Listview中的数据添加到localDB

问题描述:

我想知道如何在List#中将数据从Listview添加/插入本地数据库?谢谢。

Hi, I was wondering if how can I add/ insert data from a Listview to a local database in c#? Thanks.

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

}


浏览 ListView控件 [ ^ ]