将WPF listview值/项存储到数据库中 [英] Store WPF listview values/items into database

查看:277
本文介绍了将WPF listview值/项存储到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Wpf LisView如预览所示



现在,我有挑战将ListView的项目/值保存到数据库中,这是我用来保存项目的代码。



I have Wpf LisView As show in the Preview

Now, i'm having Challenge saving the items/values Of the ListView into the Database, This is the code i used for saving the Items.

SelectedCategory sc = new SelectedCategory();
SQLiteConnection con = new SQLiteConnection("  Data Source=database.sqlite; Version=3; Compress=True; ");
con.Open();
string query = " INSERT INTO income_details (name, amount)  VALUES (@1, @2) ";
for (int i = 0; i < ListView_Selected_Category.Items.Count; i++)
{
     SQLiteCommand cmd = new SQLiteCommand(query, con);
     cmd.Parameters.Add(new SQLiteParameter("@1", ListView_Selected_Category.Items[0]));
     cmd.Parameters.Add(new SQLiteParameter("@2", ListView_Selected_Category.Items[0]));
     cmd.ExecuteNonQuery();
}
MessageBox.Show("Saved");
con.Close(); 





因为我无法获得ListView专栏。



请我真的需要帮助,在此先感谢。



我的尝试:





Cause i cant get the ListView Column.

Please i really need help, Thanks In advance.

What I have tried:

SelectedCategory sc = new SelectedCategory();
SQLiteConnection con = new SQLiteConnection("  Data Source=database.sqlite; Version=3; Compress=True; ");
con.Open();
string query = " INSERT INTO income_details (name, amount)  VALUES (@1, @2) ";
for (int i = 0; i < ListView_Selected_Category.Items.Count; i++)
{
     SQLiteCommand cmd = new SQLiteCommand(query, con);
     cmd.Parameters.Add(new SQLiteParameter("@1", ListView_Selected_Category.Items[0]));
     cmd.Parameters.Add(new SQLiteParameter("@2", ListView_Selected_Category.Items[0]));
     cmd.ExecuteNonQuery();
}
MessageBox.Show("Saved");
con.Close();

推荐答案

您好,只需将您的 ListView 项目转换为对象其类型,现在您可以从转换的对象访问类别名称,金额,折扣属性。只需参考下面的代码,

Hi, just convert your ListView Items to object of its type, and now you can access Category Name, Amount, Discount properties from converted object. just refer the code below,
for (int i = 0; i < ListView_Selected_Category.Items.Count; i++)
{
     YourCategoryClass CategoryObject = (YourCategoryClass)ListView_Selected_Category.Items[i];
     SQLiteCommand cmd = new SQLiteCommand(query, con);
     cmd.Parameters.Add(new SQLiteParameter("@1", CategoryObject.CategoryName));
     cmd.Parameters.Add(new SQLiteParameter("@2", CategoryObject.Amount));
     cmd.ExecuteNonQuery();
}


这篇关于将WPF listview值/项存储到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆