vb中使用COMMAND对象操作数据库
'这个函数的作用是利用COMMAND对象连接数据库
操作一个SQL语句,返回一个Recordset的对象
Function CmdRunRS(ByVal strSP) '
Dim rs As New ADODB.Recordset
Dim Cmd As New ADODB.Command
Cmd.ActiveConnection = GetConnectionString() '连接字符串
Cmd.CommandText = strSP
Cmd.CommandType = adCmdText
' Execute the query for readonly
rs.CursorLocation = adUseClient
rs.Open Cmd, , , adLockReadOnly
Set Cmd.ActiveConnection = Nothing
Set Cmd = Nothing
Set rs.ActiveConnection = Nothing
' 返回一个rs记录集
Set CmdRunRS = rs
Exit Function
End Function
'这个函数的作用是利用COMMAND对象连接数据库
操作一个SQL语句,执行一个例如insert 、update、delete操作等等
Function CmdRunSQL(ByVal strSP)
On Error GoTo Error_Msg
Dim Cmd As New ADODB.Command
Cmd.ActiveConnection = GetConnectionString()
Cmd.CommandText = strSP
Cmd.CommandType = adCmdText
Cmd.Execute , , adExecuteNoRecords