Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在vb.net中运行SQL update语句,_Sql_Sql Server_Vb.net_Sql Update - Fatal编程技术网

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

无法在vb.net中运行SQL update语句,,sql,sql-server,vb.net,sql-update,Sql,Sql Server,Vb.net,Sql Update,我目前在Visual Studio 2013 Express中使用SQL Server后端。我正在尝试执行一个非常简单的update语句,但由于某些原因,该语句无法运行。它开始让我不明白这个简单的查询将如何运行 此表单用于将零件号从一台机器移动到另一台机器。我有3个组合框,组合框1是FromMachine,组合框2是to machine,组合框3是零件号 我的SQL Server表中有一列用于零件号和切割机。所有这些都在按钮单击事件上运行。请注意,零件号是日期时间,机器是整数。这是我的密码 'S

我目前在Visual Studio 2013 Express中使用SQL Server后端。我正在尝试执行一个非常简单的update语句,但由于某些原因,该语句无法运行。它开始让我不明白这个简单的查询将如何运行

此表单用于将零件号从一台机器移动到另一台机器。我有3个组合框,组合框1是FromMachine,组合框2是to machine,组合框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
简而言之,combobox 1是从中移动零件号的机器,combobox 2也是移动aprt的机器。第一条sql语句检查该零件是否已在该机器上,因为同一零件永远不会在同一机器上切割两次。然后,如果datagridview返回没有任何内容,则第二条sql语句将运行update语句

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

-----更新------

代码确实在运行,我已经完成了,但是,一旦代码完成,就不会对SQL Server进行任何更改。我已经将SQL update语句放入Management Studio并插入值,我在vb.net程序中停止了监视,它不会改变机器。它的行为就像在编写代码,只是没有将其应用于SQL Server

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

您将参数添加到comm1而不是comm2,并执行错误的命令。感谢@clweeks的评论。

使用AddsqlParameterWithType.value=value,而不是AddWithValue,后者从参数值推断可能不正确的类型。没有什么问题跑你有错误吗?你有没有检查过密码?@Tim我检查过密码,没有收到错误消息。一旦代码运行,我在服务器上再次检查它,它就不会做出更改。我已经为所有变量添加了一个表,并且在sql语句运行时它们都是正确的。@TimSchmelter我不确定sql参数的类型,你能给我一个示例,其中有一行代码,以便我可以尝试插入它。请看一下文档,你也可以使用。但请注意,您在select查询中使用的是Part,在更新中使用的是PARTNORD。在MessageBox中。显示您甚至正在使用thrd选项Combobox3.SelectedText。哪一个是正确的?并执行comm1。就是这样。那太尴尬了。