简体   繁体   English

C#删除数据表中的行

[英]C# deleting rows in DataTable

I delete rows in a DataTable with this code: 我使用以下代码删除DataTable中的行:

if (x == "y" || x == "yes")           
{             
    dt.Rows.RemoveAt(id);  
}

How I can save changes (delete row) in MS Access? 如何在MS Access中保存更改(删除行)? I have code like this, but this doest not work 我有这样的代码,但这不起作用

OleDbCommand delCmd = new OleDbCommand("DELETE FROM PERSONA WHERE ID=?",con1);
delCmd.Parameters.Add(new OleDbParameter("@ID",OleDbType.Integer, @ID));

foreach (DataRow dro in dt.GetChanges().Rows)           
{
   if (dro.RowState == DataRowState.Deleted)                                                   
   {    
      con1.Open();    
      delCmd.ExecuteNonQuery();    
      con1.Close();
   }    
}

You're not specifying the correct ID. 您没有指定正确的ID。 You need to add the parameter to the command within your loop. 您需要在循环中将参数添加到命令中。

Delete the rows using the Delete method. 使用Delete方法删除行。 This will mark the rows to be deleted when using Update on a table adapter. 这将标记在表适配器上使用更新时要删除的行。 You are just removing rows from the collection; 您只是从集合中删除行; not from the database. 不是从数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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