2014-04-07 38 views
0

我得到:DELETE FROM combobox.text數據庫

語法錯誤FROM子句;

cmdbox_Model.Text正在顯示錶的名稱。

下面是代碼

我想直接從組合框中選擇表名。因此,用戶選擇一個模型類型,直接從該表中刪除。

string Product = cmdbox_Product1.Text; 
    string Model = cmdbox_Model.Text; 
    string MacID = txt_MAC_id.Text; 

    if (conn.State == ConnectionState.Open && (cmdbox_Product1.Text == "MODEM" && cmdbox_Model.Text == Model)) 
    { 
     OleDbCommand cmd3 = new OleDbCommand("DELETE FROM " + cmdbox_Model.Text + "WHERE MacID = @MacID", conn); 

     cmd3.Parameters.AddWithValue("@MacID", MacID); 

     try 
     { 
       DialogResult result = MessageBox.Show("Are You Sure you Want to Delete this \""+ cmdbox_Product1.Text + "\"?", "Confirm DELETE",MessageBoxButtons.YesNo, MessageBoxIcon.Warning); 
       if (result == DialogResult.Yes) 
       { 
        cmd3.ExecuteNonQuery(); 


        MessageBox.Show("Successful Deleted", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); 
       } 

       else MessageBox.Show("Failed To Delete", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); } 

       conn.Close(); 
     } 
     catch (OleDbException expe) 
     { 
       MessageBox.Show(expe.Message); 
       MessageBox.Show("Error Failed to delete", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); 
       conn.Close(); 
     } 
+0

在'WHERE MacID = @ MacID'之前加一個空格' –

+0

1.在where子句前加一個空格2.使用參數以避免SQL注入漏洞 – paqogomez

+0

調制解調器模型的名稱是什麼?它是否包含空格? – Steve

回答

2

您缺少表名和WHERE之間的空格。

試試這個:

OleDbCommand cmd3 = new OleDbCommand("DELETE FROM " + cmdbox_Model.Text + " WHERE [email protected]", conn); 
+1

沒有提及sql注入嗎? – paqogomez

+0

不需要:) ............ –

0

你去哪兒包括在查詢表名?

"DELETE FROM " + cmdbox_Model.Text + "WHERE [email protected]" 

您沒有提到的表名。

DELETE 
FROM TABLE NAME 
WHERE 

是你如何編寫查詢。

相關問題