注文の詳細に複数のアイテムを含む注文を挿入すると、最後の注文の詳細アイテムのみが挿入されます。
次のコードを使用してデータベースに挿入しています
Try
order_id = value.ID
If order_id = 0 Then
cmd = New SQLiteCommand(insert_order, conn)
cmd.Parameters.AddWithValue("@customer_id", value.CustomerID)
cmd.Parameters.AddWithValue("@status", value.Status)
cmd.Parameters.AddWithValue("@deliver", value.Deliver)
cmd.Parameters.AddWithValue("@created", Now.ToString("yyyy-MM-dd"))
cmd.Parameters.AddWithValue("@deleted", "0")
order_id = Convert.ToInt32(cmd.ExecuteScalar())
Else
cmd = New SQLiteCommand(update_order, conn)
cmd.Parameters.AddWithValue("@customer_id", value.CustomerID)
cmd.Parameters.AddWithValue("@status", value.Status)
cmd.Parameters.AddWithValue("@deliver", value.Deliver)
cmd.Parameters.AddWithValue("@created", Now.ToString("yyyy-MM-dd"))
cmd.Parameters.AddWithValue("@deleted", "0")
cmd.Parameters.AddWithValue("@id", order_id)
cmd.ExecuteNonQuery()
'// delete previous order details
cmd = New SQLiteCommand(delete_order_details, conn)
cmd.Parameters.AddWithValue("@id", order_id)
cmd.ExecuteNonQuery()
End If
'// Add order details
For Each itm As typOrderDetail In value.orderDetails
cmd = New SQLiteCommand(insert_order_details, conn)
cmd.Parameters.AddWithValue("@cloth_id", itm.ClothID)
cmd.Parameters.AddWithValue("@order_id", order_id)
cmd.Parameters.AddWithValue("@color", itm.Color)
cmd.Parameters.AddWithValue("@desc", itm.Desc)
cmd.Parameters.AddWithValue("@qty", itm.Qty)
cmd.Parameters.AddWithValue("@cost", itm.Cost)
cmd.Parameters.AddWithValue("@service_id", itm.ServiceID)
cmd.Parameters.AddWithValue("@deleted", "0")
cmd.ExecuteNonQuery()
Next
sqlTrans.Commit()
'If bSave Then sqlTrans.Commit() Else sqlTrans.Rollback()
Catch ex As Exception
'sqlTrans.Rollback()
Throw
Finally
cmd.Dispose()
conn.Dispose()
End Try
ここにSQL文字列があります
INSERT INTO orders ([customer_id],[status],[deliver],[created],[deleted]) VALUES (@customer_id,@status,@deliver,@created,@deleted);select last_insert_rowid();
INSERT INTO orders_details ([order_id],[service_id],[cloth_id],[desc],[deleted],[qty],[color],[cost]) VALUES (@order_id,@service_id,@cloth_id,@desc, @deleted,@qty,@color,@cost)
Plsは私が間違っているのですか?