How to update ms access table

How to update ms access table containing single column with multiple rows. the table is mapped to listview

, and whenever I remove a row from listview

, then the row has to be removed from the table. How can i do this. Using C #. I created an oledb connection and I am removing the marked line from the list. This is the code:

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

      

Now how can I update the ms acesss table?

+1


a source to share


1 answer


Just create an OleDB command that executes a SQL DELETE statement on a row.

EDIT:
Should a table only contain one column? And this column is what you show in the list? 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 named OleDBConnection connection. Just replace tablename with your table name, colname with the column name, and YourRowValue with the value you want to remove, and you should have something that works.

To remove all checked items, you must encapsulate the code above in a method, for example 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); 
}

      

+1


a source







All Articles