ACCESS的System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误

时间:2021-10-24 08:07:33

一直用的是SQL 数据库,突然改用Access了,使用起来就是没有SQL 顺畅,老是出来些意想不到的错误。今天用Access做的网站程序进行添加数据,调试了一下午,总是异常……

提示ACCESS的System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误,刚才才在网络上找到了解决的方法,觉得有点不可思议~~在网络上看了看,也是学习ADO.net的人常犯的错误。所以写此日志,以提醒自己

    /// <summary>
/// 增加一条数据
/// </summary>
public int Add(Model.article_comment model)
{
int newId;
using (OleDbConnection conn = new OleDbConnection(DbHelperOleDb.connectionString))
{
conn.Open();
using (OleDbTransaction trans = conn.BeginTransaction())
{
try
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into " + databaseprefix + "article_comment(");
strSql.Append("channel_id,article_id,parent_id,user_id,user_name,user_ip,content,is_lock,add_time,is_reply,reply_content,reply_time,tel,zhengjian,zhengjianNum,workadd,address,youzhengbm,email,type,title,purpose,describe,newsway,getway)");
strSql.Append(" values (");
strSql.Append("@channel_id,@article_id,@parent_id,@user_id,@user_name,@user_ip,@content,@is_lock,@add_time,@is_reply,@reply_content,@reply_time,@tel,@zhengjian,@zhengjianNum,@workadd,@address,@youzhengbm,@email,@type,@title,@purpose,@describe,@newsway,@getway)"); OleDbParameter[] parameters = {
new OleDbParameter("@channel_id", OleDbType.Integer,),
new OleDbParameter("@article_id", OleDbType.Integer,),
new OleDbParameter("@parent_id", OleDbType.Integer,),
new OleDbParameter("@user_id", OleDbType.Integer,), new OleDbParameter("@user_name", OleDbType.VarChar,),
new OleDbParameter("@user_ip", OleDbType.VarChar,),
new OleDbParameter("@content", OleDbType.VarChar),
new OleDbParameter("@is_lock", OleDbType.Integer,), new OleDbParameter("@add_time", OleDbType.Date),
new OleDbParameter("@is_reply", OleDbType.Integer,),
new OleDbParameter("@reply_content", OleDbType.VarChar),
new OleDbParameter("@reply_time", OleDbType.Date), new OleDbParameter("@tel", OleDbType.VarChar,),
new OleDbParameter("@zhengjian", OleDbType.VarChar,),
new OleDbParameter("@zhengjianNum", OleDbType.VarChar,),
new OleDbParameter("@workadd", OleDbType.VarChar,), new OleDbParameter("@address", OleDbType.VarChar,),
new OleDbParameter("@youzhengbm", OleDbType.VarChar,),
new OleDbParameter("@email", OleDbType.VarChar,),
new OleDbParameter("@type", OleDbType.VarChar,), new OleDbParameter("@title", OleDbType.VarChar,),
new OleDbParameter("@purpose", OleDbType.VarChar,),
new OleDbParameter("@describe", OleDbType.VarChar,),
new OleDbParameter("@newsway", OleDbType.VarChar,),
new OleDbParameter("@getway", OleDbType.VarChar,)};
parameters[].Value = model.channel_id;
parameters[].Value = model.article_id;
parameters[].Value = model.parent_id;
parameters[].Value = model.user_id;
parameters[].Value = model.user_name;
parameters[].Value = model.user_ip;
parameters[].Value = model.content;
parameters[].Value = model.is_lock;
parameters[].Value = model.add_time;
parameters[].Value = model.is_reply;
parameters[].Value = model.reply_content;
parameters[].Value = model.reply_time;
parameters[].Value = model.tel;
parameters[].Value = model.zhengjian;
parameters[].Value = model.zhengjianNum;
parameters[].Value = model.workadd;
parameters[].Value = model.address;
parameters[].Value = model.youzhengbm;
parameters[].Value = model.email;
parameters[].Value = model.type;
parameters[].Value = model.title;
parameters[].Value = model.purpose;
parameters[].Value = model.describe;
parameters[].Value = model.newsway;
parameters[].Value = model.getway ;
DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
//取得新插入的ID
newId = GetMaxId(conn, trans);
trans.Commit();
}
catch
{
trans.Rollback();
return -;
}
}
}
return newId;
}

修改后的代码如下:

    /// <summary>
/// 增加一条数据
/// </summary>
public int Add(Model.article_comment model)
{
int newId;
using (OleDbConnection conn = new OleDbConnection(DbHelperOleDb.connectionString))
{
conn.Open();
using (OleDbTransaction trans = conn.BeginTransaction())
{
try
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into " + databaseprefix + "article_comment(");
strSql.Append("[channel_id],[article_id],[parent_id],[user_id],[user_name],[user_ip],[content],[is_lock],[add_time],[is_reply],[reply_content],[reply_time],[tel],[zhengjian],[zhengjianNum],[workadd],[address],[youzhengbm],[email],[type],[title],[purpose],[describe],[newsway],[getway])");
strSql.Append(" values (");
strSql.Append("@channel_id,@article_id,@parent_id,@user_id,@user_name,@user_ip,@content,@is_lock,@add_time,@is_reply,@reply_content,@reply_time,@tel,@zhengjian,@zhengjianNum,@workadd,@address,@youzhengbm,@email,@type,@title,@purpose,@describe,@newsway,@getway)"); OleDbParameter[] parameters = {
new OleDbParameter("@channel_id", OleDbType.Integer,),
new OleDbParameter("@article_id", OleDbType.Integer,),
new OleDbParameter("@parent_id", OleDbType.Integer,),
new OleDbParameter("@user_id", OleDbType.Integer,), new OleDbParameter("@user_name", OleDbType.VarChar,),
new OleDbParameter("@user_ip", OleDbType.VarChar,),
new OleDbParameter("@content", OleDbType.VarChar),
new OleDbParameter("@is_lock", OleDbType.Integer,), new OleDbParameter("@add_time", OleDbType.Date),
new OleDbParameter("@is_reply", OleDbType.Integer,),
new OleDbParameter("@reply_content", OleDbType.VarChar),
new OleDbParameter("@reply_time", OleDbType.Date), new OleDbParameter("@tel", OleDbType.VarChar,),
new OleDbParameter("@zhengjian", OleDbType.VarChar,),
new OleDbParameter("@zhengjianNum", OleDbType.VarChar,),
new OleDbParameter("@workadd", OleDbType.VarChar,), new OleDbParameter("@address", OleDbType.VarChar,),
new OleDbParameter("@youzhengbm", OleDbType.VarChar,),
new OleDbParameter("@email", OleDbType.VarChar,),
new OleDbParameter("@type", OleDbType.VarChar,), new OleDbParameter("@title", OleDbType.VarChar,),
new OleDbParameter("@purpose", OleDbType.VarChar,),
new OleDbParameter("@describe", OleDbType.VarChar,),
new OleDbParameter("@newsway", OleDbType.VarChar,),
new OleDbParameter("@getway", OleDbType.VarChar,)};
parameters[].Value = model.channel_id;
parameters[].Value = model.article_id;
parameters[].Value = model.parent_id;
parameters[].Value = model.user_id;
parameters[].Value = model.user_name;
parameters[].Value = model.user_ip;
parameters[].Value = model.content;
parameters[].Value = model.is_lock;
parameters[].Value = model.add_time;
parameters[].Value = model.is_reply;
parameters[].Value = model.reply_content;
parameters[].Value = model.reply_time;
parameters[].Value = model.tel;
parameters[].Value = model.zhengjian;
parameters[].Value = model.zhengjianNum;
parameters[].Value = model.workadd;
parameters[].Value = model.address;
parameters[].Value = model.youzhengbm;
parameters[].Value = model.email;
parameters[].Value = model.type;
parameters[].Value = model.title;
parameters[].Value = model.purpose;
parameters[].Value = model.describe;
parameters[].Value = model.newsway;
parameters[].Value = model.getway ;
DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
//取得新插入的ID
newId = GetMaxId(conn, trans);
trans.Commit();
}
catch
{
trans.Rollback();
return -;
}
}
}
return newId;
}

总结:在System.Data.oleDb 命名空间下使用SQL语句插入内容要在字段名与表名加“[]”