以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=55676)

--  作者:danwenxie
--  发布时间:2014/8/21 10:55:00
--  [求助]
请教一下专家,我在执行以下代码时出现一个问题:在同一个代码段中,只能执行一次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同时更新。



--  作者:Bin
--  发布时间:2014/8/21 10:59:00
--  
不执行,可能因为你Return了.  你利用messageobox.show 调试一下看看 只要执行到了Update语句就一定会更新
--  作者:Bin
--  发布时间:2014/8/21 11:00:00
--  
而且用SQL语句更新的数据, 在狐表中要重新加载才会显示
--  作者:danwenxie
--  发布时间:2014/8/21 11:00:00
--  
所有代码段一开始的定义是:
Dim cmd1 As New SQLCommand
Dim cmd2 As New SQLCommand
cmd1.C——〉这里指定外部表
cmd2.C——〉这里指定外部表
Dim dt As DataTable

复制代码时不知道发现什么问题,选择不全,重新说明一下,
[此贴子已经被作者于2014-8-21 11:01:36编辑过]

--  作者:有点甜
--  发布时间:2014/8/21 11:01:00
--  

 呃,不执行,是因为你根本就没去执行

 

cmd1.CommandText = "Update {Zzichanbiao} Set shuliang = " & shipanshu & ", shiy, quyu = \'" & quyu & "\', diqu = \'" & diqu & "\' Where bianhao = \'" & bianhao & "\'"

这一句才是执行的语句 cmd1.ExecuteNonQuery


--  作者:danwenxie
--  发布时间:2014/8/21 11:03:00
--  
谢谢您!我试试
--  作者:danwenxie
--  发布时间:2014/8/21 11:06:00
--  
谢谢有点甜大师,加上 cmd1.ExecuteNonQuery后好了。