本文介绍了将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();

推荐答案

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值/项存储到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 22:44
查看更多