将你的源程序也上传上来.让大家看看哪里有问题!!
Dim con As ADODB.Connection Dim rs As ADODB.Recordset
Private Sub db() Set con = New ADODB.Connection Set rs = New ADODB.Recordset con.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=xu;Data Source=ULSVR2;" con.Open End Sub
Private Sub Command1_Click() '删除 db Dim sql As String sql = "delete from book where id='" & Text1.Text & "' or name ='" & Text2.Text & "'" rs.Open sql, con, adOpenStatic, adLockOptimistic, adCmdText 'Dim cnt As Long 'cnt = rs.RecordCount 'If cnt = 0 Then ' MsgBox "所删除数据不在数据库中" 'Else ' MsgBox "delete" 'End If End Sub
为什么'cnt = rs.RecordCount
会出现"operation is not allowed when the object is close"请问是什么出错
我给你的另一个解决方案,使用Command对象的ExeCute方法可以得到操作影响的记录行数
Option Explicit
Dim con As ADODB.Connection
Private Sub db() Set con = New ADODB.Connection con.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=xu;Data Source=ULSVR2;" con.Open End Sub
Private Sub Command1_Click() '删除 db Dim sql As String Dim cmd As New ADODB.Command Dim cnt As Long '定义变量,返回数据操作影响的行数
sql = "delete from book where id='" & Text1.Text & "' or name ='" & Text2.Text & "'" Set cmd.ActiveConnection = con cmd.CommandText = sql cmd.Execute cnt
If cnt = 0 Then MsgBox "所删除数据不在数据库中" Else MsgBox "已成功删除" & cnt & "条数据!" End If
End Sub