简体   繁体   中英

How to update ms access table

How to update ms access table which contains single column with multiple rows. the table is showing on a listview and whenever I delete a row from listview then the row should be deleted from table. How can I do this. Using C#. I created a oledb connection and I delete a row from list view checked item. That is the code:

if (listView1.CheckedItems.Count > 0)
{
    foreach (ListViewItem lvi in listView1.CheckedItems)
       listView1.Items.Remove(lvi);
}

Now how can I update ms acesss table?

Just create an OleDB command that performs an sql DELETE query for the row.

EDIT:
Does the table contain only one column? And that column is what you are displaying in the listview? Then you can do something like this:

using (OleDBCommand deleteCommand = connection.CreateCommand())
{
  deleteCommand.CommandText = "DELETE FROM tablename WHERE colname=@rowvalue";
  deleteCommand.Parameters.AddWithValue("@rowvalue", YourRowValue);
  deleteCommand.ExecuteNonQuery();
}

This code snippet assumes that you have an open OleDBConnection named connection. Just replace tablename with the name of your table, colname with the name of the column, and YourRowValue with the value you want to delete and you should have something that works.

To delete all checked items you should encapsulate the code above in a method, say DeleteItem(string name) And use the following:

List<ListViewItem> itemsToBeDeleted = new List<ListViewItem>(listView1.CheckedItems); 
foreach (ListViewItem itemToDelete in itemsToBeDeleted)
{ 
   DeleteItem(itemToDelete.Text);
   listView1.Items.Remove(itemToDelete); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM