2013-06-27 64 views
1

我想把用戶輸入作爲參數從我的firebird數據庫中刪除某些記錄。sql,簡單查詢從db中刪除記錄

所以我下面的代碼

private void btnOk_Click(object sender, EventArgs e) 
     { 
      string code = txtDeleteField.Text; 
      string connString = ConfigurationManager.AppSettings["DemoAppConnectionString"]; 

      using (FbConnection conn = new FbConnection(connString)) 
      { 
       using (FbCommand cmd = new FbCommand()) 
       { 
        cmd.CommandText = "DELETE * FROM DEMOAPP WHERE CODE= @P1"; 
        cmd.Parameters.AddWithValue("@P1", code); 
        cmd.Connection = conn; 
        conn.Open(); 
        cmd.Transaction = conn.BeginTransaction(); 

        int a = cmd.ExecuteNonQuery(); 
        if (a == 0) 
        { 
         MessageBox.Show("Error!"); 
        } 
        else 
        { 
         MessageBox.Show("Record deleted, ok!"); 
        } 
       } 

      } 
     } 

這段代碼產生錯誤

 
An unhandled exception of type 'FirebirdSql.Data.FirebirdClient.FbException' occurred in FirebirdSql.Data.FirebirdClient.dll 

Additional information: Dynamic SQL Error 
SQL error code = -104 
Token unknown - line 1, column 8 
* 
+1

如何使用'SELECT'刪除? – manji

+0

這是你的實際碼嗎? 'SELECT'不會刪除記錄,這個錯誤似乎表明你嘗試做'DELETE * FROM',這是無效的(你應該把'*'留出。 –

+0

typo :)這是刪除,現在它已更新 – panjo

回答

1

您所查詢的是錯的,"DELETE * FROM DEMOAPP WHERE CODE= @P1"無效:應該有一個DELETE聲明沒有*

將其更改爲"DELETE FROM DEMOAPP WHERE CODE= @P1"應解決該問題。

+0

好的,謝謝..... – panjo

+1

@panjo你應該把答案標記爲正確的,如果它解決了你的問題。 – MJM