我编写了一个连接到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);
}
}
}
***********************************************************************************************
为什么程序失败?
“porgram”? “Accsses”? “excaption”?你应该使用拼写检查器。 – abelenky 2010-12-22 18:47:25
http://xkcd.com/327/ – 2010-12-22 18:48:17
[* **从不**写`扔前;`](http://stackoverflow.com/questions/2999298/difference-between-throw-and-throw-new -exception/2999314#2999314)。 – SLaks 2010-12-22 18:48:39