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"请问是什么出错
晕倒,Delete语句执行后不返回你操作所影响的记录数
RecordCount属性返回的是记录的条数,一般是通过Select查询语句,或者执行存储过程返回记录集,或者是打开表是返回记录集
我给你的另一个解决方案,使用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
能不能给我解释一下这几条语句,谢谢
Set cmd.ActiveConnection = con cmd.CommandText = sql cmd.Execute cnt
还有这条
Dim cmd As New ADODB.Command