我正在使用VB.Net创建一个Windows应用程序。 SQLite数据库。在我的一个win形式中,我曾经通过单个事件下的单独SQL命令从两个表(ImpShpHeader
,ImpShpDetail
)中检索数据。 ImpShpHeader
在ImpShpDetail
中检索DataGridView
中的数据并使用按钮单击事件使用两个单独的SQL命令更新两个表时,文本框中的数据检索一个命令是从文本框更新为ImpShpHeader
但我正在使用DataGridView
从ImpshpDetail
更新为CommandBuilder
。从文本框更新到ImpShpHeader
是成功的,但CommandBuilder
更新会出现以下错误:多个基表不支持动态SQL生成。所以这里我需要手动SQL命令来更新任何DataGridView中的更改。这是我的代码:
'TO RETRIEVE DATA FROM TABLES
Private Sub txtContainerNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtContainerNo.KeyDown
If e.KeyCode = Keys.Enter Then
Try
con.Open()
cmdString = "SELECT * FROM ImpShpHeader WHERE container = '" & txtContainerNo.Text & "'"
ImpShpHeaderDA = New SQLiteDataAdapter(cmdString, con)
ImpShpHeaderDA.Fill(ImpShpHeaderDT)
txtContainer.Text = ImpShpHeaderDT.Rows(0).Item("container").ToString
txtContainer.Enabled = False
txtShipper.Text = ImpShpHeaderDT.Rows(0).Item("shipper").ToString
txtPackages.Text = ImpShpHeaderDT.Rows(0).Item("packages").ToString
txtETA.Text = ImpShpHeaderDT.Rows(0).Item("eta").ToString
cmdString = "SELECT item, description, shipqty FROM ImpShpDetail WHERE container = '" & txtContainer.Text & "'"
ImpShpDetailDA = New SQLiteDataAdapter(cmdString, con)
ImpShpDetailDA.Fill(ImpShpDetailDT)
dgvImpShpDetail.DataSource = ImpShpDetailDT
'STYLE AND APPEARENCE OF DATA GRID VIEW
dgvImpShpDetail.Columns(0).HeaderText = "Item No."
dgvImpShpDetail.Columns(1).HeaderText = "Description"
dgvImpShpDetail.Columns(2).HeaderText = "Ship Qty"
dgvImpShpDetail.RowHeadersVisible = False
dgvImpShpDetail.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.DisplayedCells
dgvImpShpDetail.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
dgvImpShpDetail.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
dgvImpShpDetail.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Catch ex As Exception
MsgBox("Ann error occured!" & vbCrLf & vbCrLf & ex.Message)
End Try
con.Close()
End If
End Sub
'TO UPDATE MULTIPLE TABLES
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
dlgResult = MessageBox.Show("Do you want to save the changes?", "Confirmation", MessageBoxButtons.YesNo)
If dlgResult = DialogResult.Yes Then
Try
con.Open()
cmdString = "UPDATE ImpShpHeader SET container = @container, shipper = @shipper, packages = @packages, eta = @eta WHERE container = '" & txtContainer.Text & "'"
cmd = New SQLiteCommand(cmdString, con)
cmd.Parameters.Add("@container", SqlDbType.VarChar).Value = txtContainer.Text
cmd.Parameters.Add("@shipper", SqlDbType.VarChar).Value = txtShipper.Text
cmd.Parameters.Add("@packages", SqlDbType.VarChar).Value = txtPackages.Text
cmd.Parameters.Add("@eta", SqlDbType.VarChar).Value = txtETA.Text
cmd.ExecuteNonQuery()
cmdbl = New SQLiteCommandBuilder(ImpShpDetailDA)
ImpShpDetailDA.Update(ImpShpDetailDT)
MsgBox("Updated successfully!")
Catch ex As Exception
MsgBox("An error occured!" & vbCrLf & vbCrLf & ex.Message)
End Try
con.Close()
End If
End Sub