C#访问列表框 [英] c# access listbox

查看:66
本文介绍了C#访问列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库(访问)中删除多个与列表框匹配的记录(在C#中)

i want to delete from database(access) multiple records which are matching to a listbox(in c#)
so how can i do that?

推荐答案

列表框仅存储纯文本,因此您必须将有关记录的相关信息存储在另一个列表或数据集中.只要您的列表框未排序(这意味着列表框和对象列表的顺序相同),您就可以执行以下操作:

The listbox stores only plain text, so you have to store the relevant information about your records in another list or in a dataset. As long as your listbox is not sorted (that means, listbox and objects list are in same order) you can do something like that:

for(int i=0; i<listbox.Count; i++)
{
  // for another list
  // id = yourObjectList[i].id;

  //for a dataset
  id = yourDataset.Rows[i][indexOfId];

  execSql("delete from yourTable where yourId="+id);
}



附注:由于您的列表是唯一文件名的列表,因此您可以尝试以下操作:



P.S.: As your list is a list of unique filenames, you can try this:

string files = string.Empty;
string separator = "";
for each(item in listbox.items)
{
  files += separator + "''" + item + ''"'';
  separator = ", ";
}

sql = "delete from yourTable where filename in ("+files+")";
execSql(sql);


如果要删除选定的列表框项目,
If you want to delete selected listbox items
foreach (ListItem item in ListBox1.Items)
                {
                    if (item.Selected == true && item == valuefromdb)
                    {                        
                       //code to delete from db 
                    }
                }


如果要从数据库中删除列表框中的匹配项,请


If you want to delete matching items in listbox from db then

foreach (ListItem item in ListBox1.Items)
                {
                  
                    if (item == valuefromdb)
                    {
                        //code to delete from db
                    }
                }


这篇关于C#访问列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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