更新SQL数据库问题

时间:2018-03-28 00:33:21

标签: sql asp.net vb.net ms-access

非常感谢您的回复。所以我已经做了你建议的更改,但我仍然没有让我的数据库更新。我从你的代码中唯一改变的是MessageBox行。我将这些更改为警报消息。我在这里错过了什么?也许我的变量需要以不同的方式声明谢谢!!!

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


    Dim x As Integer
    Dim z As Integer
    Dim r As Integer

    Dim V1 As String
    Dim V2 As String




    x = txbPalletNumber.Text
    z = txbOrderNumber.Text
    r = txbShipmentNumber.Text


    Try
            Using conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\inetpub\wwwroot\Traceability\Traceability.accdb")
                If Not Integer.TryParse(txbPalletNumber.Text, x) Then
                Response.Write("<script type=""text/javascript"">alert(""Pallet Number must be a Number"");</script")
                Exit Sub
                End If
                If Not Integer.TryParse(txbOrderNumber.Text, z) Then
                Response.Write("<script type=""text/javascript"">alert(""Order Number must be a Number"");</script")
                Exit Sub
                End If
                If Not Integer.TryParse(txbShipmentNumber.Text, r) Then
                Response.Write("<script type=""text/javascript"">alert(""Shipment Number must be a Number"");</script")
                Exit Sub
                End If
                Using cmd As New OleDbCommand("SELECT Status FROM tblPalletRecords WHERE Palletnumber = @x ", conn)
                    cmd.Parameters.Add("@x", OleDbType.Integer).Value = x
                    conn.Open()
                    V1 = CStr(cmd.ExecuteScalar())
                conn.Close()

            End Using
                If V1 = "In Stock" Then
                    Using cmd2 As New OleDbCommand("UPDATE tblPalletRecords SET OrderNumber = @z, ShipmentNumber = @r WHERE PalletNumber = @x", conn)
                        cmd2.CommandText = "UPDATE tblPalletRecords SET OrderNumber = @z, ShipmentNumber = @r WHERE PalletNumber = @x "
                        cmd2.Parameters.Add("@z", OleDbType.Integer).Value = z
                        cmd2.Parameters.Add("@r", OleDbType.Integer).Value = r
                        cmd2.Parameters.Add("@x", OleDbType.Integer).Value = x
                        conn.Open()
                        cmd2.ExecuteNonQuery()
                        conn.Close()
                    End Using
                    Using cmd3 As New OleDbCommand("SELECT Status FROM tblPalletRecords WHERE Palletnumber = @x", conn)
                        cmd3.Parameters.Add("@x", OleDbType.Integer).Value = x
                        conn.Open()
                        V2 = CStr(cmd3.ExecuteScalar())
                        conn.Close()
                    End Using
                    Response.Write("<script type=""text/javascript"">alert(""The Status to " & x & " has Changed to " & V2 & """);</script")
                Else
                    Response.Write("<script type=""text/javascript"">alert(""The Pallet is not In Stock to Ship"");</script")
                End If
            End Using
        Catch ex As Exception
            'Error handling
        End Try

        txbSearch.Text = txbPalletNumber.Text
    GridView1.DataBind()
End Sub
End Class

4 个答案:

答案 0 :(得分:2)

在执行update语句之前,使用select语句覆盖commandtext的值,因此永远不会运行。

答案 1 :(得分:0)

您可以声明新的oledb命令,因为最后一个命令会覆盖更新命令:

Dim cmd2 as New OleDbCommand 'Set the command properties. cmd.Connection = conn cmd.CommandText = "SELECT Status FROM tblPalletRecords WHERE Palletnumber = " & x & " " V1 = cmd.ExecuteScalar() If V1 = "In Stock" Then cmd2.CommandText = "UPDATE tblPalletRecords SET OrderNumber = " & z & ", ShipmentNumber = " & r & " WHERE PalletNumber = " & x & " " cmd.CommandText = "SELECT Status FROM tblPalletRecords WHERE Palletnumber = " & x & " " V2 = cmd.ExecuteScalar() Response.Write("<script type=""text/javascript"">alert(""The Status to " & x & " has Changed to " & V2 & """);</script") Else Response.Write("<script type=""text/javascript"">alert(""The Pallet is not In Stock to Ship"");</script") End If cmd2.ExecuteNonQuery() conn.Close()

答案 2 :(得分:0)

立即开启Option Strict以及所有项目。在执行之前直接打开您的连接,然后立即关闭它。使用TryParse检查您是否具有正确的数字输入。始终使用参数。它将减少灾难性的SQL注入并使您的SQL语句更容易编写。我添加了Using..End Using语句。这将负责处理您的对象。如果在达到.Close()方法之前发生错误,它也将关闭您的连接。我本来希望重用x参数但是Access不关心参数名,只关心SQL语句中的顺序所以我每次都使用一个新命令。我已经展示了如何使用Constructors来保存几行代码;在连接构造函数中传递连接字符串,并将CommandText和Connection直接传递给Command construtor。从你的SQL语句中可以看出,x,z和r都是数字类型而不是字符串,所以我在Integer中猜到了。您可能需要更改此设置,具体取决于表中的数据类型。

Dim x As Integer
        Dim z As Integer
        Dim r As Integer
        Dim V1 As String
        Dim V2 As String
        Try
            Using conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\inetpub\wwwroot\Traceability\Traceability.accdb")
                If Not Integer.TryParse(txbPalletNumber.Text, x) Then
                    MessageBox.Show("Please enter a number for the Pallet.")
                    Exit Sub
                End If
                If Not Integer.TryParse(txbOrderNumber.Text, z) Then
                    MessageBox.Show("Please enter a number for the Order Number.")
                    Exit Sub
                End If
                If Not Integer.TryParse(txbShipmentNumber.Text, r) Then
                    MessageBox.Show("Please enter a number for the Shipment Number.")
                    Exit Sub
                End If
                Using cmd As New OleDbCommand("SELECT Status FROM tblPalletRecords WHERE Palletnumber = @x ", conn)
                    cmd.Parameters.Add("@x", OleDbType.Integer).Value = x
                    conn.Open()
                    V1 = CStr(cmd.ExecuteScalar())
                    conn.Close()
                End Using
                If V1 = "In Stock" Then
                    Using cmd2 As New OleDbCommand("UPDATE tblPalletRecords SET OrderNumber = @z, ShipmentNumber = @r WHERE PalletNumber = @x", conn)
                        cmd2.CommandText = "UPDATE tblPalletRecords SET OrderNumber = @z, ShipmentNumber = @r WHERE PalletNumber = @x "
                        cmd2.Parameters.Add("@z", OleDbType.Integer).Value = z
                        cmd2.Parameters.Add("@r", OleDbType.Integer).Value = r
                        cmd2.Parameters.Add("@x", OleDbType.Integer).Value = x
                        conn.Open()
                        cmd2.ExecuteNonQuery()
                        conn.Close()
                    End Using
                    Using cmd3 As New OleDbCommand("SELECT Status FROM tblPalletRecords WHERE Palletnumber = @x", conn)
                        cmd3.Parameters.Add("@x", OleDbType.Integer).Value = x
                        conn.Open()
                        V2 = CStr(cmd3.ExecuteScalar())
                        conn.Close()
                    End Using
                    Response.Write("<script type=""text/javascript"">alert(""The Status to " & x & " has Changed to " & V2 & """);</script")
                Else
                    Response.Write("<script type=""text/javascript"">alert(""The Pallet is not In Stock to Ship"");</script")
                End If
            End Using
        Catch ex As Exception
            'Error handling
        End Try

答案 3 :(得分:0)

你需要首先学习编码标准,声明像x,y,z这样的变量,我很久没见过了。

您正在覆盖cmd.CommandText

cmd.CommandText = "UPDATE tblPalletRecords SET OrderNumber = " & z & ", ShipmentNumber = " & r & " WHERE PalletNumber = " & x & " "
            cmd.CommandText = "SELECT Status FROM tblPalletRecords WHERE Palletnumber = " & x & " "
            V2 = cmd.ExecuteScalar()

修复:

 cmd.CommandText = "UPDATE tblPalletRecords SET OrderNumber = " & z & ", ShipmentNumber = " & r & " WHERE PalletNumber = " & x & " "
cmd.ExecuteNonQuery()