2010-12-22 144 views
0

我編寫了一個連接到MS Access的程序。當我填寫字段並添加一個新項目訪問該程序失敗。例外是「INSERT INTO語句中的語法錯誤」INSERT INTO語句中的語法錯誤

下面是相關的代碼。

**************************************************************** 
AdoHelper.cs 
**************************************************************** 

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Data; 
using System.Data.OleDb; 

namespace Yad2 
{ 
    class AdoHelper 
    { 
     //get the connection string from the app.config file 
     //Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Yad2.accdb 
     static string connectionString = Properties.Settings.Default.DBConnection.ToString(); 

     //declare the db connection 
     static OleDbConnection con = new OleDbConnection(connectionString); 

     /// <summary> 
     /// To Execute queries which returns result set (table/relation) 
     /// </summary> 
     /// <param name="query">the query string</param> 
     /// <returns></returns> 
     public static DataTable ExecuteDataTable(string query) 
     { 

      try 
      { 
       con.Open(); 
       OleDbCommand command = new OleDbCommand(query, con); 
       System.Data.OleDb.OleDbDataAdapter tableAdapter = new System.Data.OleDb.OleDbDataAdapter(command); 
       DataTable dt = new DataTable(); 
       tableAdapter.Fill(dt); 
       return dt; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      con.Close(); 
     } 
    } 

    /// <summary> 
    /// To Execute update/insert/delete queries 
    /// </summary> 
    /// <param name="query">the query string</param> 
    public static void ExecuteNonQuery(string query) 
    { 
     try 
     { 
      con.Open(); 
      System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(query, con); 
      command.ExecuteNonQuery(); 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      con.Close(); 
     } 
    } 

    /// <summary> 
    /// To Execute queries which return scalar value 
    /// </summary> 
    /// <param name="query">the query string</param> 
    public static object ExecuteScalar(string query) 
    { 
     try 
     { 
      con.Open(); 
      System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(query, con); /// here is the Excaption !!!!!!!!! 
      return command.ExecuteScalar(); 
     } 
     catch 
     { 
      throw; 
     } 
     finally 
     { 
      con.Close(); 
     } 
    } 

} 
} 

**************************************************************************** 


**************************************************************************** 
DataQueries.cs 
**************************************************************************** 
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Data; 

namespace Yad2 
{ 
    class DataQueries 
    { 

     public static DataTable GetAllItems() 
     { 
      try 
      { 
       string query = "Select * from Messages"; 

       DataTable dt = AdoHelper.ExecuteDataTable(query); 

       return dt; 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 


     public static void AddNewItem(string mesNumber, string title , string mesDate , string contactMail , string mesType , string Details) 
     { 
      string query = "Insert into Messages values(" + mesNumber + " , '" + title + "' , '" + mesDate + "' , '" + contactMail + "' , , '" + mesType + "' , '" + Details + "')"; 
      AdoHelper.ExecuteNonQuery(query); 
     } 

     public static void DeleteDept(int mesNumber) 
     { 
      string query = "Delete from Item where MessageNumber=" + mesNumber; 
      AdoHelper.ExecuteNonQuery(query); 
     } 
    } 
} 
*********************************************************************************************** 

爲什麼程序失敗?

+0

「porgram」? 「Accsses」? 「excaption」?你應該使用拼寫檢查器。 – abelenky 2010-12-22 18:47:25

+2

http://xkcd.com/327/ – 2010-12-22 18:48:17

+2

[* **從不**寫`扔前;`](http://stackoverflow.com/questions/2999298/difference-between-throw-and-throw-new -exception/2999314#2999314)。 – SLaks 2010-12-22 18:48:39

回答

4

當你把你的字符串放入SQL時,你會得到一個無效的語法。
如果其中一個字符串包含',則會發生這種情況。

您需要使用參數。

此外,您的SQL包含, ,,這是無效的語法。

0

爲什麼不簡單地打印出query的值(到調試窗口,控制檯,消息框,日誌文件......無論哪裏!),在AddNewItem中,然後檢查該消息。那真的應該變得清楚了。

4

試試這個

INSERT INTO table (column1, column2, ...) 
VALUES ('value1', 'value2', ...) 
1
string query = "Insert into Messages values(" + mesNumber + " , '" + title + "' , '" + mesDate + "' , '" + contactMail + "' , , '" + mesType + "' , '" + Details + "')"; 

產生

Insert into Messages 
values(
    <number> , 
    '<title>' , 
    '<mesDate>' , 
    '<contactMail>' , , 
    '<mesType>' , 
    '<Details>' 
) 

注意與他們之間的空間後,兩個逗號。這不是有效的SQL。如果mesNumber在您的代碼中爲空值,您也將有一個錯誤的查詢。

正如Joe White評論他的鏈接到XKCD#327,總是消毒您的數據庫輸入!這意味着如果一個字符串傳遞給你的方法,你必須轉義所有的單引號。

由於SLaks評論,從未使用throw ex;,只需使用throw;

相關問題