请教一下专家,我在执行以下代码时出现一个问题:在同一个代码段中,只能执行一次UPDATE,请专家大师们为我指点迷津!谢谢!谢谢!
代码1:
Dim cmd1 As New SQLCommand
Dim cmd2 As New SQLCommand
cmd1.C
cmd2.C
Dim dt As DataTable
Dim shenheren As String = "test"
Dim danhao As String = e.Form.Controls("danhao").value
Dim shipanshu As Integer
Dim shiyongren As String
Dim quyu As String
Dim diqu As String
Dim bianhao As String
If danhao = "" Then
Messagebox.show("请输入盘点单号!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Return
End If
cmd1.CommandText = "Select * From {Zpandianbiao} Where danhao = '" & danhao & "' "
If cmd1.ExecuteScalar < 1 Then
Messagebox.show("修改失败,找不到此盘点记录!","提 示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Return
Else
dt = cmd1.ExecuteReader
cmd1.CommandText = "Update {Zpandianbiao} Set [shenheren] = '" & shenheren & "' Where [danhao] = '" & danhao & "'"
End If
查询SQL数据表Zpandianbiao时,发现表中已有更新。
---------------------
代码2:
Dim cmd1 As New SQLCommand
Dim cmd2 As New SQLCommand
cmd1.C
cmd2.C
Dim dt As DataTable
Dim shenheren As String = "test"
Dim danhao As String = e.Form.Controls("danhao").value
Dim shipanshu As Integer
Dim shiyongren As String
Dim quyu As String
Dim diqu As String
Dim bianhao As String
If danhao = "" Then
Messagebox.show("请输入盘点单号!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Return
End If
cmd1.CommandText = "Select * From {Zpandianbiao} Where danhao = '" & danhao & "' "
If cmd1.ExecuteScalar < 1 Then
Messagebox.show("修改失败,找不到此盘点记录!","提 示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Return
Else
dt = cmd1.ExecuteReader
cmd1.CommandText = "Update {Zpandianbiao} Set [shenheren] = '" & shenheren & "' Where [danhao] = '" & danhao & "'"
End If
For Each dr As DataRow In dt.DataRows
bianhao = dr("bianhao")
shipanshu = dr("shipanshu")
shiyongren = dr("shiyongren")
quyu = dr("quyu")
diqu = dr("diqu")
cmd2.CommandText = "Update {Zzichanbiao} Set shuliang = " & shipanshu & ", shiy, quyu = '" & quyu & "', diqu = '" & diqu & "' Where bianhao = '" & bianhao & "'"
Next
If cmd2.ExecuteNonQuery > 0 Then '返回1表示更改成功
Messagebox.show("审核成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Tables("盘点表_Table1").DataTable.Load()
Vars("_AddEdit")=0
e.Form.Controls("修改").Enabled = True
e.Form.Controls("删除").Enabled = True
e.Form.Controls("保存").Enabled = False
e.Form.Controls("取消").Enabled = False
Else
Messagebox.show("更改失败!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
End If
查询SQL数据表Zpandianbiao、Zzichanbiao时,发现两个表都没有更新。
---------------------
代码2:
Dim cmd1 As New SQLCommand
Dim cmd2 As New SQLCommand
cmd1.C
cmd2.C
Dim dt As DataTable
Dim shenheren As String = "test"
Dim danhao As String = e.Form.Controls("danhao").value
Dim shipanshu As Integer
Dim shiyongren As String
Dim quyu As String
Dim diqu As String
Dim bianhao As String
If danhao = "" Then
Messagebox.show("请输入盘点单号!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Return
End If
cmd1.CommandText = "Select * From {Zpandianbiao} Where danhao = '" & danhao & "' "
If cmd1.ExecuteScalar < 1 Then
Messagebox.show("修改失败,找不到此盘点记录!","提 示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Return
Else
dt = cmd1.ExecuteReader
End If
For Each dr As DataRow In dt.DataRows
bianhao = dr("bianhao")
shipanshu = dr("shipanshu")
shiyongren = dr("shiyongren")
quyu = dr("quyu")
diqu = dr("diqu")
cmd1.CommandText = "Update {Zzichanbiao} Set shuliang = " & shipanshu & ", shiy, quyu = '" & quyu & "', diqu = '" & diqu & "' Where bianhao = '" & bianhao & "'"
Next
If cmd2.ExecuteNonQuery > 0 Then '返回1表示更改成功
Messagebox.show("审核成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Tables("盘点表_Table1").DataTable.Load()
Vars("_AddEdit")=0
e.Form.Controls("修改").Enabled = True
e.Form.Controls("删除").Enabled = True
e.Form.Controls("保存").Enabled = False
e.Form.Controls("取消").Enabled = False
Else
Messagebox.show("更改失败!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
End If
查询SQL数据表Zzichanbiao时,发现符合条件的记录只有最后一个有更新。
---------------------
我希做到按下按键时,表Zpandianbiao的所有指定条件(单号为指定单号)的记录更新一下审核人信息,并在表Zzichanbiao中对应的记录中更新相应的多个字段。但目前无法做到表Zpandianbiao和表Zzichanbiao同时更新。