如何在sql数据库中存储listview行的内容? [英] How to store the content of a listview row in an sql database?

查看:86
本文介绍了如何在sql数据库中存储listview行的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有一个程序可以在发生某些事情时更新审计跟踪(列表视图),例如用户登录,创建/删除新用户以及等等。

列表视图有10列,列有:日期,时间,消息描述,事件类型等。

我需要存储每个listview条目( SQL数据库中的原始子项目每当我用新的子项更新listView时。我找不到listView ItemsChanged事件,所以我使用了ItemSelectionChanged事件。但是,如何在每个ItemSelectionChanged事件中将插入行的这些子项复制到数据库中?如何获取行的每个子项的内容,以便我可以将它们复制到数据库中?



请帮助

Hi guys

I have a program that updates an Audit Trail (a listview) when something happens, like a user login, create/delete a new user and so on.
The listview has 10 columns, with columns like: Date, Time, Message Description, Event type and so on.
I need to store each listview entry (subItems of a raw) in an SQL Database Whenever i update the listView with new subItems. I couldn't find a listView ItemsChanged event, so I used the ItemSelectionChanged event. But how can i copy these subItems of an inserted row into the database at each ItemSelectionChanged event? How can i get the contents of each subItems of a row so that i can copy them into a database?

Please help

推荐答案

谢谢你们的帮助,我设法解决了这个问题。

在listViewQuick_ItemSelectionChanged事件中,我设法从列表视图中复制数据,如下所示:



int countRows = 0;

private void listViewQuick_ItemSelectionChanged(object sender,ListViewItemSelectionChangedEventArgs e)

{

Thank you guys for your assistance, i managed to resolve the problem.
In the listViewQuick_ItemSelectionChanged event, i managed to copy data from the list view like this:

int countRows = 0;
private void listViewQuick_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
listViewQuick.FullRowSelect = true;
                int[] count = new int[10];;
                con = new SqlConnection(cs.DBConn);
                con.Open();

               string cb = "insert into AuditTrail (Date,Time,Location,MessageDescription) VALUES (@d1,@d2,@d3,@d4)";
               cmd = new SqlCommand(cb);
               cmd.Connection = con;

               cmd.Parameters.Add("@d1", System.Data.SqlDbType.VarChar);
               cmd.Parameters.Add("@d2", System.Data.SqlDbType.VarChar);
               cmd.Parameters.Add("@d3", System.Data.SqlDbType.VarChar);
               cmd.Parameters.Add("@d4", System.Data.SqlDbType.VarChar);
               

               cmd.Parameters["@d1"].Value = (string)listViewQuick.SelectedItems[countRows].SubItems[0].Text;
               cmd.Parameters["@d2"].Value = (string)listViewQuick.SelectedItems[countRows].SubItems[1].Text;
               cmd.Parameters["@d3"].Value = (string)listViewQuick.SelectedItems[countRows].SubItems[2].Text;
               cmd.Parameters["@d4"].Value = (string)listViewQuick.SelectedItems[countRows].SubItems[3].Text;
               

               countRows++;

               cmd.ExecuteReader();
               con.Close();



}


}


这篇关于如何在sql数据库中存储listview行的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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