我有一个datagridview,我只想保存产品代码,还有文本框; totalamount,现金,变化。如何将产品代码和文本框值保存到数据库?我尝试了下面的代码,但它只保存了一行。谢谢!
代码:
Public Sub addTransaction()
Try
dbConnection()
insert_query = "INSERT INTO tbltransactions(trans_number, trans_total, trans_cash, trans_change, trans_date, trans_time, trans_month, trans_day, trans_year, invoice_number, product_code) VALUES(@trans_number, @trans_total, @trans_cash, @trans_change, @trans_date, @trans_time, @trans_month, @trans_day, @trans_year, @trans_invoice, @product_code);"
command = New SqlCommand
With command
.Connection = connection
.CommandText = insert_query
.Parameters.Clear()
.Parameters.Add(New SqlParameter With {.ParameterName = "@trans_number", .SqlDbType = SqlDbType.VarChar, .Value = CashierView.txtTransactionID.Text})
.Parameters.Add(New SqlParameter With {.ParameterName = "@trans_total", .SqlDbType = SqlDbType.VarChar, .Value = CashierView.txtTotalAmount.Text})
.Parameters.Add(New SqlParameter With {.ParameterName = "@trans_cash", .SqlDbType = SqlDbType.VarChar, .Value = CashierView.txtTendered.Text})
.Parameters.Add(New SqlParameter With {.ParameterName = "@trans_change", .SqlDbType = SqlDbType.VarChar, .Value = CashierView.txtChange.Text})
.Parameters.Add(New SqlParameter With {.ParameterName = "@trans_date", .SqlDbType = SqlDbType.VarChar, .Value = Date.Now.ToString("MMMM dd, yyyy")})
.Parameters.Add(New SqlParameter With {.ParameterName = "@trans_time", .SqlDbType = SqlDbType.VarChar, .Value = TimeOfDay.ToString("hh:mm:ss tt")})
.Parameters.Add(New SqlParameter With {.ParameterName = "@trans_month", .SqlDbType = SqlDbType.VarChar, .Value = Date.Now.ToString("MMMM")})
.Parameters.Add(New SqlParameter With {.ParameterName = "@trans_day", .SqlDbType = SqlDbType.VarChar, .Value = Date.Now.ToString("dd")})
.Parameters.Add(New SqlParameter With {.ParameterName = "@trans_year", .SqlDbType = SqlDbType.VarChar, .Value = Date.Now.ToString("yyyy")})
.Parameters.Add(New SqlParameter With {.ParameterName = "@trans_invoice", .SqlDbType = SqlDbType.VarChar, .Value = CashierView.lblInvoiceNumber.Text})
.Parameters.Add(New SqlParameter With {.ParameterName = "@product_code", .SqlDbType = SqlDbType.VarChar})
For Each row As DataGridViewRow In CashierView.ProductsListDGV.Rows
If Not row.IsNewRow Then
.Parameters("@product_code").Value = row.Cells(0).Value
.ExecuteNonQuery()
End If
Next
End With
Catch ex As SqlException
MsgBox("Error: " + ex.Message)
Finally
connection.Close()
command.Dispose()
End Try
End Sub