【发布时间】:2010-12-22 18:42:25
【问题描述】:
我编写了一个连接到 MS Access 的程序。当我填写字段并向 Access 添加新项目时,程序失败。例外是“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"? “访问”? “例外”?您应该使用拼写检查器。
-
@abelenky:对某些人来说,英语很困难。因此,我们可以使用编辑按钮来帮助清理它。
-
@Chris:ESL 作者尤其应该使用拼写检查器。计算机会做拼写,所以我们不必这样做。