2010-12-22 145 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;

相关问题