没有生成有效的deletecommand或者没有主键。可以在保证表含有主键的情况下,添加一句:
dim cmd as new sqlcommand("select * from test",conn)
dim sqldpr as new sqldataadapter(cmd)
Dim sqlb As New SqlCommandBuilder(sqldpr) ‘就是这句。
然后再操作!
可以使用
DataRow[] adr = table.select("age>50)
foreach ( DataRow dr in adr)
dr.delete;
的方法,这样更简单!
当然不用都循环了。
Dim tbl As DataTable = CType(Me.NotCResizegrid1.DataSource, DataTable)
Dim view As New DataView(tbl, "id>7", "id", DataViewRowState.CurrentRows)
Dim i As Integer
Dim r As DataRowView
For Each r In view
Dim row As DataRow = r.Row
row.Delete()
Next
Dim p As DataTable = tbl.GetChanges
If Not p Is Nothing Then
Sqldpr.Update(p)
End If
' Set PrimaryKey
customerTable.Columns("id").Unique = true
customerTable.PrimaryKey = new DataColumn() { customerTable.Columns("id") }
' add ten rows
Dim id As Integer
For id = 1 To 10
customerTable.Rows.Add( _
new object() { id, string.Format("customer{0}", id) } )
Next id
customerTable.AcceptChanges()
' add another ten rows
For id = 11 To 20
customerTable.Rows.Add( _
new object() { id, string.Format("customer{0}", id) } )
Next id
Dim strExpr As String
Dim strSort As String
strExpr = "id > 5"
' Sort descending by CompanyName column.
strSort = "name DESC"
' Use the Select method to find all rows matching the filter.
Dim foundRows As DataRow() = _
customerTable.Select( strExpr, strSort, DataViewRowState.Added )
PrintRows( foundRows, "filtered rows")
foundRows = customerTable.Select()
PrintRows( foundRows, "all rows")
End Sub
Dim cmd As New SqlCommand("delete from test where age>50", conn)
Dim returncount As Integer
Try
conn.open()
returncount = cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
Return
Finally
conn.close()
End Try
If returncount > 0 Then
MessageBox.Show("ok!")
End If