不知道你还会不会在呢? 我也是一个新手,很愿意和你交流一下.
我到是实现了删除数据,以下是我的代码,你先参考一下:
Imports System.Data.OleDb
Public Class Form1
Const AssociatorInfoSql As String = "SELECT * FROM AssociatorInfo ORDER BY UserID"
Const ConsumeInfoSql As String = "SELECT * FROM ConsumeInfo ORDER BY UserID"
Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Environment.CurrentDirectory & "\Associator.mdb"
Dim XTQConn As New OleDbConnection(ConnString)
Dim XTQAdapter As OleDbDataAdapter
Dim XTQDataSet As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
XTQConn.Open()
'填充Associatorinfo表
XTQAdapter = New OleDbDataAdapter(AssociatorInfoSql, XTQConn)
XTQDataSet.Tables.Add("AssInfoTable") '添加一个表ASSINFOTABLE在xtqdataset中
XTQAdapter.Fill(XTQDataSet, "AssInfoTable")
XTQAdapter.Dispose()
XTQAdapter = New OleDbDataAdapter(ConsumeInfoSql, XTQConn)
XTQDataSet.Tables.Add("ConsumeInfoTable")
XTQAdapter.Fill(XTQDataSet, "ConsumeInfoTable")
XTQAdapter.Dispose()
XTQConn.Close()
DGViewAss.DataSource = XTQDataSet.Tables("AssInfoTable")
dgViewConsume.DataSource = XTQDataSet.Tables("ConsumeInfoTable")
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
DGViewAss.Rows.Remove(DGViewAss.CurrentRow)
SaveChanges()
End Sub
Private Sub SaveChanges()
Try
If XTQDataSet.HasChanges Then
XTQDataSet.AcceptChanges()
XTQConn.Open()
XTQAdapter = New OleDbDataAdapter(AssociatorInfoSql, XTQConn)
Dim Builder As New OleDbCommandBuilder(XTQAdapter)
XTQAdapter.Update(XTQDataSet.Tables("AssInfotable"))
MessageBox.Show("保存成功")
Builder.Dispose()
XTQConn.Close()
btnSaveChange.Enabled = False
End If
Catch ex As Exception
MessageBox.Show("保存数据时错误,请检查程序状态!", "保存数据", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Finally
XTQConn.Close()
End Try
End Sub
End Class
我想,你的代码改成这样试试,看行不行:
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If (Me.BindingContext(objDataSet1, "ad_class").Count > 0) Then
Me.BindingContext(objDataSet1, "ad_class").RemoveAt(Me.BindingContext(objDataSet1, "ad_class").Position)
Me.objDataSet1_PositionChanged()
objDataSet1.AcceptChanges() '这是我加的
Dim da As New OleDbDataAdapter(Str, cnnstr)
Dim db As New OleDbCommandBuilder(da)
da.Update(objDataSet1, "ad_class")
End If
End Sub
OleDbDataAdqpter对象的两个参数中,第一个参数是SQL命令字符串,第二个参数是OleDbConnection对象.
我在我的机子上,没有我加的那句,也是会出错的.
对了,我用的是VS2005