如何保存Listview项目&子项目 [英] How Do I Save Listview Items & Subitems

查看:53
本文介绍了如何保存Listview项目&子项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如何保存listview项目&子文件在.db文件(数据库)和更新当前数据库并加载它



或以xml保存/更新/加载。



我在谷歌搜索并且只找到保存列表视图使用txt文件。

Hi guys,
how to save listview items & subitems in .db file ( data base) and with update current database and load it

or save/update/load in xml.

I serach in google and only find save listview With txt file.

推荐答案

那么你没有显示你有多少列或者它们是什么类型,所以我只能提供一般性建议。

我对你的数据库结构一无所知。



你可以用一种方法解决这个问题的方法是创建一个反映数据库结构的 DataTable

Well you don't show how many columns you have or what type they are, so I can only give general advice.
Also I know nothing about your database structure.

One way you can solve this problem is to create a DataTable that reflects the structure in your database.
DataTable dt = new DataTable();
dt.Columns.Add("Column1", typeof(int));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", typeof(double));





Then您在循环中浏览列表视图并向数据表中添加行。



Then you go through the list view in a loop and add rows to your data table.

foreach (ListViewItem item in listView1.Items)
{
    DataRow drNew = dt.NewRow();
    drNew["Column1"] = int.Parse(item.SubItems[0].Text);
    drNew["Column2"] = item.SubItems[0].Text;
    drNew["Column3"] = double.Parse(item.SubItems[0].Text);
    dt.Rows.Add(drNew);
}
dt.AcceptChanges();





如果你想把数据保存为XML,你只需要使用此代码行:



If you want to save the data as XML, you just use this code line:

dt.WriteXml(@"C:\SomeFolder\SomeFileName.xml");





这是保存数据的一种简单方法进入数据库



This is one simplistic way to save the data into the database

string sql = "INSERT INTO table_name (Column1, Column2, Column3) VALUES (@col1, @col2, @col3)";
using (SqlConnection conn = new SqlConnection(connectionString))
{
   conn.Open();
   foreach (DataRow dr in dt.Rows)
   {
       SqlCommand cmd = conn.CreateCommand();
       cmd.CommandText = sql;
       cmd.Parameters.AddWithValue("@col1", dr["Column1"]);
       cmd.Parameters.AddWithValue("@col2", dr["Column2"]);
       cmd.Parameters.AddWithValue("@col3", dr["Column3"]);
       cmd.ExecuteNonQuery();
   }
}





还有很多其他方法可以做到这一点。



There are many other ways to do this.


你可以编写一个数据访问层方法来将数据保存到数据库中,并将其引用到当前项目中。然后在想要保存列表项时调用该dal方法。
hai you can write one data access layer method to save data into data base and and add reference this to your current project. then call that dal method whenever you want to save list items.


这篇关于如何保存Listview项目&子项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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