无法在vb.net中运行SQL更新语句,

时间:2015-05-04 14:28:09

标签: sql sql-server vb.net sql-update

我目前正在使用SQL Server后端在Visual Studio 2013 Express中工作。我试图做一个非常简单的更新声明,但由于某种原因,这件事不会运行。我开始对这个简单的查询不会运行的方式感到沮丧。

此表单应该将部件号从一台机器移动到另一台机器。我有3个组合框,combobox1是FromMachine,combobox2是机器,组合框3是部件号。

我的SQL Server表有一个部件号和切割机的列。所有这些都是在按钮点击事件上运行的。请注意,部件号是日期时间,机器是整数。这是我的代码。

'Safety check
If Combobox1.SelectedIndex = -1 Then
        MsgBox("Please select a machine to move the Part from.")
        Exit Sub
End If

If Combobox2.SelectedIndex = -1 Then
        MsgBox("Please select a machine to move the part to.")
        Exit Sub
End If

If Combobox3.SelectedIndex = -1 Then
        MsgBox("Please select a part number to change from one cutting machine to another.")
        Exit Sub
End If

    Dim FromMachine As Integer = Combobox1.SelectedValue
    Dim ToMachine As Integer = Combobox2.SelectedValue
    Dim Part As DateTime = Combobox3.SelectedValue

    Try

        'sql command for the change
        'make sure that part isnt already on that machine
        Using conn1 As New SqlConnection(connstring)
            conn1.Open()
            Using comm1 As New SqlCommand("SELECT PartNumber FROM Table1 Where PartNumber = @PartNumber AND Machine = @ToMachine", conn1)
                comm1.Parameters.AddWithValue("PartNumber", Part)
                comm1.Parameters.AddWithValue("@ToMachine", ToMachine)
                Dim dt As New DataTable
                Dim sql As New SqlDataAdapter(comm1)
                sql.Fill(dt)
                DataGridView1.DataSource = dt
                'check for data entries
                If dt.Rows.Count > 0 Then
                    MsgBox("Part Number " & Combobox3.SelectedText & " is already assigned to " & Combobox2.SelectedText & ". This Part could not be changed.")
                    Exit Sub
                Else
                    'Make the change
                    Using conn2 As New SqlConnection(connstring)
                        conn2.Open()
                        Using comm2 As New SqlCommand("UPDATE table1 SET Machine = @ToMachine Where Machine = @FromMachine " _
                                                       & "AND PartNumber = @PartNumber", conn1)
                            comm1.Parameters.AddWithValue("@ToMachine", ToMachine)
                            comm1.Parameters.AddWithValue("@FromMachine", FromMachine)
                            comm1.Parameters.AddWithValue("@PartNumber", PartNumber)
                            comm1.ExecuteNonQuery()
                            MsgBox("PartNumber " & Combobox3.SelectedText & " has been changed from cutting machine " & Combobox1.SelectedText & " To cutting machine " & Combobox2.SelectedText & ".")
                        End Using
                        conn2.Close()
                    End Using
                End If
            End Using
            conn1.Close()
        End Using

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

简而言之,组合框1是部件号移动的机器,而combobox2也是移动的机器。第一个sql语句检查该部件是否已经在该机器上,因为相同的部件永远不会在同一台机器上被切割两次。然后,如果datagridview返回没有任何内容,则第二个sql语句将运行update语句。

此外,所有组合框都是从SQL查询加载的,当我添加观察它们或将SQL语句插入我的可视化管理程序时,它们都具有正确的值。

---------- UPDATE -------------

代码确实运行并且我已经完成了它,但是,一旦代码完成,就不会对SQL Server进行任何更改。我已将SQL更新语句放入Management Studio并插入值,我在我的vb.net程序中关闭了手表,它不会更改机器。它表现得像是在执行代码,但只是不将其应用于SQL Server。

------------更新------------- 错误是将comm1参数用于comm2并在comm2下执行comm1。

1 个答案:

答案 0 :(得分:3)

您正在将参数添加到comm1而不是comm2并执行错误的命令(感谢@clweeks发表评论)。