2013-07-08 47 views
1

我使用SQL Server在我的數據庫刪除查詢在C#不工作

這裏是代碼

private void btnDelete_Click(object sender, EventArgs e) 
     { 
      try 
      { 
        //GlobalClass.dt.Rows[rowId].Delete(); 
        //GlobalClass.adap.Update(GlobalClcass.dt); 

       cDatabaseSQLServer.Delete("satuan", "WHERE id = " + rowId + ""); 
        //this.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 
     } 

public bool Delete(String tableName, String where) 
     { 
      switch (sqlType) 
      { 
       case DATABASE_SQL_TYPE.DATABASE_SQL_TYPE_SQLITE: 
        return cSQLite.Delete(tableName, where); 
       case DATABASE_SQL_TYPE.DATABASE_SQL_TYPE_MSSQL: 
        return cSQL.Delete(tableName, where); 
      } 
      return false; 
     } 

public bool Delete(String tableName, String where) 
     { 
      Boolean returnCode = true; 
      try 
      { 
       this.ExecuteNonQuery(String.Format("delete from {0} where {1};", tableName, where)); 
      } 
      catch (Exception fail) 
      { 
       MessageBox.Show(fail.Message); 
       returnCode = false; 
      } 
      return returnCode; 
     } 

當我調試應用程序,刪除不工作和數據仍然在datagridview的存在,如何解決這個問題?

+1

看起來你將有兩個結束連續的「WHERE」。也使用參數化查詢。 –

+3

你在'Delete'函數中有一個很大的安全漏洞。如果我通過了';放下所有'作爲我的'哪裏'參數? –

+0

'cDatabaseSQLServer.Delete(「satuan」,「WHERE id =」+ rowId +「」);'似乎'WHERE'關鍵字在查詢中發送了2次....刪除'WHERE'關鍵字 –

回答

2

您所查詢的是錯的,你有2次where,要麼改變你的方法調用或查詢創建者:

cDatabaseSQLServer.Delete("satuan", "id = " + rowId + ""); //remove where from here 

,因爲它是在這裏:

this.ExecuteNonQuery(String.Format("delete from {0} where {1};", tableName, where)); 
+0

是我的錯,我忘了在我的查詢中刪除WHERE。 –