2016-10-18 16 views
1

我正在使用DataGridView,我想從網格更新我的表格。當使用DataAdapter.Update(dataTable)時,出現「不支持多個基表的動態SQL生成」錯誤。原因是因爲我通過連接2個表填充了我的數據表。如何以及在哪裏可以修改DataAdapter中的自動生成的查詢?

這是我用來從網格更新數據庫的功能。

private void button2_Click(object sender, EventArgs e) 
    { 
     dataGridView2.EndEdit(); 
     //da.Update(dataTable); 
     OleDbCommand com = new OleDbCommand(); 
     com.Connection = connection;    
     com.CommandText = "update Name_Corpus2 set EngWord = @EngWord where ID = @ID"; 
     com.Parameters.Add("@ID", OleDbType.Integer, 32, "ID"); 
     com.Parameters.Add("@EngWord", OleDbType.VarChar, 64, "EngWord"); 
     da.UpdateCommand = com; 
     da.Update(dataTable); 
     MessageBox.Show("Updated"); 
     bind(classification, language);    
    } 

據我所知,我將不得不創建我自己的查詢來更新表格從網格。我想知道如何以及在哪裏輸入代碼來自動生成我的更新查詢。

+0

你應該與它的CommandText和Parameters創建自己的SqlCommand,然後將其設置爲DataAdapter的 – Steve

+0

@Steve感謝快速響應的UpdateCommand屬性。所以我做了你說的,我得到一個錯誤。已經在問題中更新了我的功能。當我嘗試更新表格時,它說「必須聲明標量變量」@EngWord「。」 – yash1309

+0

在@ @ID之前添加'@ EngWord'參數(IE遵循查詢中佔位符的確切順序,這對OleDB是必需的) – Steve

回答

0

這是我更新的函數,通過從OleDB命令切換到SqlCommand併爲其添加commandtext和參數來自動生成更新查詢。謝謝@Steve

private void button2_Click(object sender, EventArgs e) 
    { 
     dataGridView2.EndEdit(); 
     SqlCommand com = new SqlCommand(); 
     com.Connection = con; 
     com.Parameters.Add("@EngWord", SqlDbType.NVarChar, 256, "EngWord"); 
     com.Parameters.Add("@ID", SqlDbType.Int, 32, "ID"); 
     com.CommandText = "update Name_Corpus2 set EngWord = @EngWord where ID = @ID"; 

     da.UpdateCommand = com; 
     da.Update(dataTable); 
     MessageBox.Show("Updated"); 
     bind(classification, language);    
    } 
相關問題