关于ADODB.Command的问题

JustStruggle 2004-08-13 12:51:21
当用Command.Execute进行SQL语句之后,如删除一条记录
请问怎样更新数据库呢
...全文
156 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
bboos 2004-08-14
  • 打赏
  • 举报
回复
多用户情况下,用事务处理比较好。
On Error GoTo err002
strsql = "delete * from 表 where 编号=" & LV_select & ""
GDb.BeginTrans
GDb.Execute (strsql)
If MsgBox("数据删除将无法恢复" & Chr(13) & Chr(13) & "是否进行本次操作?", vbQuestion + vbYesNo, "提示") = vbYes Then
GDb.CommitTrans
Else
GDb.RollbackTrans
End If
Exit Sub
err002:
GDb.RollbackTrans
MsgBox "本次操作失败"
kingnew 2004-08-14
  • 打赏
  • 举报
回复
直接用con.execute Runsql就行了
羽毛之家 2004-08-14
  • 打赏
  • 举报
回复
学习
chenyu5188 2004-08-14
  • 打赏
  • 举报
回复
cn.CursorLocation=adUseClient
RunSql = "delete from item where 编号=" & "'" & CStr(Index) & "'"
set Comm.ActiveConnection=cn
Comm.CommandText = RunSql
Comm.Execute
'由于delete是不是返回记录集的语句,所以记录集对象不要更新

asdf2002 2004-08-14
  • 打赏
  • 举报
回复
With Cmd
.ActiveConnection = Con
.CommandText = StrDataCreate'SQL语句变量
.CommandType = adCmdText
End With
Set Rs = Cmd.Execute
Rs.Update

看这样行不行.
JustStruggle 2004-08-13
  • 打赏
  • 举报
回复
我顶
JustStruggle 2004-08-13
  • 打赏
  • 举报
回复
不行啊,我是这样做的:
RunSql = "delete from item where 编号=" & "'" & CStr(Index) & "'"
Comm.CommandText = RunSql
Comm.Execute
Rs.Update
但那条“被”删除的记录还在数据库里面啊
请问怎么办啊,还是我的语句有问题?,谢谢
talent303 2004-08-13
  • 打赏
  • 举报
回复
Command.Execute("delete from table where ......")
删除以后,,也可以用来更新记录集的方法,,
记录集.update
Andy__Huang 2004-08-13
  • 打赏
  • 举报
回复
用兩句Command.Execute嗎
Command.Execute("delete from table where ......")
Command.Execute("update table set field1='xx',field2='yy'....")
部分源代码: Private Sub Command1_Click() Dim sql As String Dim param As ADODB.Parameter Dim cmd As ADODB.Command Set cmd = New ADODB.Command Set param = New ADODB.Parameter sql = "select * from book where [图书编码]= ? " If Command1.Caption = "添加记录" Then Command1.Caption = "确 定" Command2.Enabled = False '屏蔽删除、修改、下一记录、上一记录按钮,避免出现数据库错误 Command3.Enabled = False Command4.Enabled = False cmdmodify.Enabled = False rst.AddNew Else If Text1.Text = "" Then frmMsg.Show frmMsg.notice.Visible = True frmMsg.Text1.Text = "图书编号不能为空!" Command2.Enabled = True Command3.Enabled = True Command4.Enabled = True Command1.Caption = "添加记录" Exit Sub End If With param .Direction = adParamInput .Type = adBSTR .Size = 8 .Value = Text1.Text End With cmd.Parameters.Append param cmd.CommandText = sql cmd.CommandType = adCmdText Set cmd.ActiveConnection = con Set rst1 = cmd.Execute If rst1.RecordCount > 0 Then frmMsg.Show frmMsg.notice.Visible = True frmMsg.Text1.Text = "此书号已经存在!" rst.Cancel Command2.Enabled = True Command3.Enabled = True Command4.Enabled = True cmdmodify.Enabled = True Command1.Caption = "添加记录" Command2.Enabled = True Exit Sub End If rst.Update Command2.Enabled = True '数据库更新结束后才可以点击其他几个按钮 Command3.Enabled = True Command4.Enabled = True cmdmodify.Enabled = True Command1.Caption = "添加记录" End If End Sub 修改图书记录操作 Private Sub cmdmodify_Click() '修改记录按钮 Dim sqlstr As String Dim rst1 As New ADODB.Recordset Dim param As ADODB.Parameter Dim cmd As ADODB.Command Set cmd = New ADODB.Command Set param = New ADODB.Parameter sqlstr = "select * from book where [图书编码]= '" & Trim(Text1.Text) & "'" With param .Direction = adParamInput .Type = adBSTR .Size = 8 .Value = Text1.Text End With cmd.Parameters.Append param cmd.CommandText = sqlstr cmd.CommandType = adCmdText Set cmd.ActiveConnection = con Set rst1 = cmd.Execute If rst1.RecordCount > 0 Then frmMsg.Show frmMsg.notice.Visible = True frmMsg.Text1.Text = "此书号已经存在!" rst.Cancel Exit Sub End If rst.Update frmMsg.Show frmMsg.info.Visible = True frmMsg.Text1.Text = "修改成功!" End Sub 添加职工信息 Private Sub Command1_Click() '添加记录 Dim sql As String Dim param As ADODB.Parameter Dim cmd As ADODB.Command Set cmd = New ADODB.Command Set param = New ADODB.Parameter sql = "select * from worker where [工号]= ? " If Command1.Caption = "添加记录" Then Command1.Caption = "确 定" Command2.Enabled = False Command3.Enabled = False Command4.Enabled = False rst.AddNew Else If Text1.Text = "" Then frmMsg.Show frmMsg.notice.Visible = True frmMsg.Text1.Text = "职工编号不能为空!" Command2.Enabled = True Command3.Enabled = True Command4.Enabled = True Command1.Caption = "添加记录" Exit Sub End If With param .Direction = adParamInput .Type = adBSTR .Size = 8 .Value = Text1.Text End With cmd.Parameters.Append param cmd.CommandText = sql cmd.CommandType = adCmdText Set cmd.ActiveConnection = con Set rst1 = cmd.Execute '检测职工编号是否存在防止主键冲突 If rst1.RecordCount > 0 Then frmMsg.Show frmMsg.notice.Visible = True frmMsg.Text1.Text = "此职工编号已经存在!" rst.Cancel Command2.Enabled = True Command3.Enabled = True Command4.Enabled = True Command1.Caption = "添加记录" Exit Sub End If rst.Update Command2.Enabled = True Command3.Enabled = True Command4.Enabled = True Command1.Caption = "添加记录" End If End Sub

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧