我有四张桌子。此代码的目的是让用户从组合框中选择一个客户,并检查发往该客户的货物以放入FormHeaderTable。让我们假装选择两个货物......表格可以有多个货件。出货可以有几个零件号
表1: FormHeaderTable
表2: FormLineTable
表3: ShipmentHeaderTable
表4: ShipmentLineTable
我需要根据从客户组合框选择填充的核对表框中选择的FormLineTable
向我的ShipmentNumbers
插入值。
我试着这样做:
Dim OrderKey As Integer
Dim LastFormID As Integer
Dim NextFormID As Integer
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Get FormID and Get OrderKey
Dim MAXOrderKey As String = "SELECT MAX(OrderKey) FROM FormHeaderTable WHERE CustomerName = @CustomerName"
Dim MAXOrderKeyCommand As New SqlCommand(MAXOrderKey, con)
Dim MAXFormID As String = "Select MAX(FormID) FROM FormHeaderTable"
Dim MAXFormIDCommand As New SqlCommand(MAXFormID, con)
MAXOrderKeyCommand.Parameters.Add("@CustomerName", SqlDbType.VarChar).Value = cboCustomer.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
LastFormID = CInt(MAXFormIDCommand.ExecuteScalar)
Catch ex As System.Exception
LastFormID = 0
End Try
Try
OrderKey = CInt(MAXOrderKeyCommand.ExecuteScalar)
Catch ex As System.Exception
OrderKey = 0
End Try
con.Close()
NextFormID = LastFormID + 1
OrderKey = OrderKey + 1
Dim cmd As New SqlCommand("SET IDENTITY_INSERT FormHeadertable ON Insert INTO FormHeaderTable (FormID, CustomerName, CreationDate, OrderKey) Values (@FormID, @CustomerName, @CreationDate, @OrderKey) SET IDENTITY_INSERT FormHeadertable OFF ", con)
cmd.Parameters.Add("@FormID", SqlDbType.Int).Value = NextFormID
cmd.Parameters.Add("@CustomerName", SqlDbType.VarChar).Value = cboCustomer.Text
cmd.Parameters.Add("@CreationDate", SqlDbType.Date).Value = Date.Now()
cmd.Parameters.Add("@OrderKey", SqlDbType.Int).Value = OrderKey
'this is where I need help'
Dim cmd1 As New SqlCommand("Insert into FormLineTable (FormID, OrderKey, LineId, ShipmentNumber, PartNumber) Select FormID, OrderKey, ShipmentLineNumber, ShipmentNumber, PartNumber FROM ShipmentLineTable CROSS JOIN FormHeaderTable", con)
cmd1.Parameters.Add("@FormID", SqlDbType.Int).Value = NextFormID
cmd1.Parameters.Add("@OrderKey", SqlDbType.Int).Value = OrderKey
Dim isFirst As Boolean = True
For i As Integer = 0 To chkLstShipments.Items.Count - 1
If chkLstShipments.GetItemCheckState(i) = CheckState.Checked Then
If isFirst = True Then
cmd1.CommandText += " WHERE ShipmentNumber = @ShipmentNumber" + i.ToString
isFirst = False
Else
cmd1.CommandText += " OR ShipmentNumber = @ShipmentNumber" + i.ToString
End If
cmd1.Parameters.Add("@ShipmentNumber" + i.ToString, SqlDbType.Int).Value = chkLstShipments.Items(i)
End If
Next
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
cmd1.ExecuteNonQuery()
con.Close()
End Sub
我的错误
36 duplicate values or so...
答案 0 :(得分:0)
摆脱了交叉加入部分。将FormId和OrderKey切换到变量@FormId和@OrderKey,并使用我在代码中先前定义的变量填充这两个变量:NextNaftaId和OrderKey
新代码如下所示:
Dim cmd1 As New SqlCommand("Insert into FormLineTable (FormID, OrderKey, LineId, ShipmentNumber, PartNumber) Select @FormID, @OrderKey, ShipmentLineNumber, ShipmentNumber, PartNumber FROM ShipmentLineTable", con)
cmd1.Parameters.Add("@FormID", SqlDbType.Int).Value = NextFormID
cmd1.Parameters.Add("@OrderKey", SqlDbType.Int).Value = OrderKey