2016-11-18 71 views
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”列,因此我只需更新该字段。

+0

检查是否有帮助http://stackoverflow.com/questions/1666425/using-datagridview-to-update-multiple-tables –

+0

@phonetic_man,感谢您的链接。我的假设可能是正确的,SQL应该用JOIN构造。虽然我无法弄清楚正确的合成它:( – LuckyLuke82

回答

0

这对我来说太复杂了,看起来像Oracle提供的使用数据集的建议并不容易,当您想要在连接表记录上执行更新时并不容易。所以我尝试了一种不同的方法,它为我工作。因为我需要的是更新从SQL查询返回到datagridview的我这样做只有1列:

For Each row As DataGridViewRow In My_.Rows 

     cmd.Parameters.Add(New OracleParameter("validity", row.Cells(6).Value)) 
     cmd.CommandText = "UPDATE TABLE3 SET VALIDITY= : validity WHERE ID_TABLE1_FK='" & row.Cells(1).Value & "'" 

     cmd.ExecuteNonQuery() 
     cmd.Parameters.Clear() 

Next 

如果有人知道答案我原来的问题 - 那么如何做同一只da.Update(DS .Tables(0)),那么请让我知道。我认为SQL查询需要适当地改变,使用JOIN方法。