0
我正在使用Datagridview來顯示我從2個表中加入記錄。顯示的數據來自其中一個表+已加入表中的數據(表3)。 SQL查詢返回Datagridview中的結果(在Oracle中也能正常工作),但更新失敗,因爲「動態SQL生成失敗。未找到基表或找到多於一個基表」。這裏是我的表設計:Datagridview - Oracle更新錯誤「動態SQL生成失敗。」
Table1:
ID_TABLE1
ITEM_NAME
ITEM_DESCRIPTION
Table3: (this is a joined view for Table1 and Table2)
ID_TABLE3
ID_TABLE1_FK
ID_TABLE3_FK
VALIDITY
DATE_CONNECTION
我的代碼(完全一樣,Oracle建議):
Public Class Form2
Private da As OracleDataAdapter
Private cb As OracleCommandBuilder
Private ds As DataSet
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Saving.Enabled = False 'this deals with error with updating (from Oracle site)
Dim SQL As String = "SELECT ID_TABLE1, ID_TABLE3, SERIAL_NO, ITEM_NAME, ITEM_DESCRIPTION, VALIDITY, DATE_CONNECTION from TABLE1, TABLE2 WHERE TABLE3.ID_TABLE1_FK=" & Form1.DataGridView1.CurrentRow.Cells(0).Value.ToString
Try
Oracleconn() 'connection to my DB
Dim cmd = New OracleCommand(SQL, Oracleconn)
cmd.CommandType = CommandType.Text
da = New OracleDataAdapter(cmd)
cb = New OracleCommandBuilder(da)
ds = New DataSet()
da.Fill(ds)
My_DGV.DataSource = ds.Tables(0)
Saving.Enabled = True
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'No closing of connection here because of working with Dataset (Oracle suggestion)
End Try
End Sub
Private Sub Saving
da.Update(ds.Tables(0))
Saving.Enabled = True
End Sub
End Class
是這樣,我的SQL查詢錯誤還是什麼?任何幫助將非常感激 !
P.S .:在實際情況下,只允許用戶更改Table3中的「VALIDITY」列,因此我只需更新該字段。
檢查是否有幫助http://stackoverflow.com/questions/1666425/using-datagridview-to-update-multiple-tables –
@phonetic_man,感謝您的鏈接。我的假設可能是正確的,SQL應該用JOIN構造。雖然我無法弄清楚正確的合成它:( – LuckyLuke82