0

我在我的项目中创建了 DataTable。用数据库中的表填充它。

Dim myDataTable As New DataTable(Table_Name)

Dim myLocalConnection As New SqlConnection(_ConnectionString)
myLocalConnection.Open()

Dim mySqlDataAdapter As New SqlDataAdapter()
myCommand.Connection = myLocalConnection
mySqlDataAdapter.SelectCommand = myCommand
mySqlDataAdapter.Fill(myDataTable)

现在我想从 tableby 条件中删除特定行。并更新数据库中的表。用SqlDataAdapter.Update()方法。

mySqlDataAdapter.Update(myDataTable)

我怎么能这样做?帮助...

4

2 回答 2

0

使用类似的东西

foreach (DataRow row in myDataTable.Rows)
    if (row ...) // implement your condition here
        myDataTable.Rows.Remove(row);

这里是 C# 代码,因为您在标题中要求 C#。

于 2013-10-30T11:15:27.940 回答
0

您必须提供DeleteCommand. DataAdapter然后你可以使用DataRow.Delete它来改变它的行状态,RowState.Deleted当你使用DataAdapter.Update(myDataTable).

要查找要删除的行,您可以使用Linq-To-DataSet

Dim deleteCommand = new SqlCommand("DELETE FROM ...", myLocalConnection)
mySqlDataAdapter.DeleteCommand = deleteCommand 

Dim query = From row In myDataTable
            Where row.Field(Of String)("ColumnName") = "Delete-Condition"
For Each row As DataRow in query 
    row.Delete()
Next

mySqlDataAdapter.Update(myDataTable)
于 2013-10-30T11:17:36.577 回答