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

一直用的是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,4),
                                new OleDbParameter("@article_id", OleDbType.Integer,4),
                                new OleDbParameter("@parent_id", OleDbType.Integer,4),
                                new OleDbParameter("@user_id", OleDbType.Integer,4),

                                new OleDbParameter("@user_name", OleDbType.VarChar,100),
                                new OleDbParameter("@user_ip", OleDbType.VarChar,255),
                                new OleDbParameter("@content", OleDbType.VarChar),
                                new OleDbParameter("@is_lock", OleDbType.Integer,4),

                                new OleDbParameter("@add_time", OleDbType.Date),
                                new OleDbParameter("@is_reply", OleDbType.Integer,4),
                                new OleDbParameter("@reply_content", OleDbType.VarChar),
                                new OleDbParameter("@reply_time", OleDbType.Date),

                                new OleDbParameter("@tel", OleDbType.VarChar,30),
                                new OleDbParameter("@zhengjian", OleDbType.VarChar,30),
                                new OleDbParameter("@zhengjianNum", OleDbType.VarChar,50),
                                new OleDbParameter("@workadd", OleDbType.VarChar,30),

                                new OleDbParameter("@address", OleDbType.VarChar,30),
                                new OleDbParameter("@youzhengbm", OleDbType.VarChar,30),
                                new OleDbParameter("@email", OleDbType.VarChar,30),
                                new OleDbParameter("@type", OleDbType.VarChar,30),

                                new OleDbParameter("@title", OleDbType.VarChar,50),
                                new OleDbParameter("@purpose", OleDbType.VarChar,100),
                                new OleDbParameter("@describe", OleDbType.VarChar,255),
                                new OleDbParameter("@newsway", OleDbType.VarChar,30),
                                new OleDbParameter("@getway", OleDbType.VarChar,30)};
                        parameters[0].Value = model.channel_id;
                        parameters[1].Value = model.article_id;
                        parameters[2].Value = model.parent_id;
                        parameters[3].Value = model.user_id;
                        parameters[4].Value = model.user_name;
                        parameters[5].Value = model.user_ip;
                        parameters[6].Value = model.content;
                        parameters[7].Value = model.is_lock;
                        parameters[8].Value = model.add_time;
                        parameters[9].Value = model.is_reply;
                        parameters[10].Value = model.reply_content;
                        parameters[11].Value = model.reply_time;
                        parameters[12].Value = model.tel;
                        parameters[13].Value = model.zhengjian;
                        parameters[14].Value = model.zhengjianNum;
                        parameters[15].Value = model.workadd;
                        parameters[16].Value = model.address;
                        parameters[17].Value = model.youzhengbm;
                        parameters[18].Value = model.email;
                        parameters[19].Value = model.type;
                        parameters[20].Value = model.title;
                        parameters[21].Value = model.purpose;
                        parameters[22].Value = model.describe;
                        parameters[23].Value = model.newsway;
                        parameters[24].Value = model.getway ;
                        DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
                        //取得新插入的ID
                        newId = GetMaxId(conn, trans);
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return -1;
                    }
                }
            }
            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,4),
                                new OleDbParameter("@article_id", OleDbType.Integer,4),
                                new OleDbParameter("@parent_id", OleDbType.Integer,4),
                                new OleDbParameter("@user_id", OleDbType.Integer,4),

                                new OleDbParameter("@user_name", OleDbType.VarChar,100),
                                new OleDbParameter("@user_ip", OleDbType.VarChar,255),
                                new OleDbParameter("@content", OleDbType.VarChar),
                                new OleDbParameter("@is_lock", OleDbType.Integer,4),

                                new OleDbParameter("@add_time", OleDbType.Date),
                                new OleDbParameter("@is_reply", OleDbType.Integer,4),
                                new OleDbParameter("@reply_content", OleDbType.VarChar),
                                new OleDbParameter("@reply_time", OleDbType.Date),

                                new OleDbParameter("@tel", OleDbType.VarChar,30),
                                new OleDbParameter("@zhengjian", OleDbType.VarChar,30),
                                new OleDbParameter("@zhengjianNum", OleDbType.VarChar,50),
                                new OleDbParameter("@workadd", OleDbType.VarChar,30),

                                new OleDbParameter("@address", OleDbType.VarChar,30),
                                new OleDbParameter("@youzhengbm", OleDbType.VarChar,30),
                                new OleDbParameter("@email", OleDbType.VarChar,30),
                                new OleDbParameter("@type", OleDbType.VarChar,30),

                                new OleDbParameter("@title", OleDbType.VarChar,50),
                                new OleDbParameter("@purpose", OleDbType.VarChar,100),
                                new OleDbParameter("@describe", OleDbType.VarChar,255),
                                new OleDbParameter("@newsway", OleDbType.VarChar,30),
                                new OleDbParameter("@getway", OleDbType.VarChar,30)};
                        parameters[0].Value = model.channel_id;
                        parameters[1].Value = model.article_id;
                        parameters[2].Value = model.parent_id;
                        parameters[3].Value = model.user_id;
                        parameters[4].Value = model.user_name;
                        parameters[5].Value = model.user_ip;
                        parameters[6].Value = model.content;
                        parameters[7].Value = model.is_lock;
                        parameters[8].Value = model.add_time;
                        parameters[9].Value = model.is_reply;
                        parameters[10].Value = model.reply_content;
                        parameters[11].Value = model.reply_time;
                        parameters[12].Value = model.tel;
                        parameters[13].Value = model.zhengjian;
                        parameters[14].Value = model.zhengjianNum;
                        parameters[15].Value = model.workadd;
                        parameters[16].Value = model.address;
                        parameters[17].Value = model.youzhengbm;
                        parameters[18].Value = model.email;
                        parameters[19].Value = model.type;
                        parameters[20].Value = model.title;
                        parameters[21].Value = model.purpose;
                        parameters[22].Value = model.describe;
                        parameters[23].Value = model.newsway;
                        parameters[24].Value = model.getway ;
                        DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
                        //取得新插入的ID
                        newId = GetMaxId(conn, trans);
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return -1;
                    }
                }
            }
            return newId;
        }

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

时间: 2024-10-23 09:32:46

ACCESS的System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误的相关文章

异常详细信息: System.Data.OleDb.OleDbException: FROM 子句语法错误

异常详细信息: System.Data.OleDb.OleDbException: FROM 子句语法错误,但是SQL语句单独执行没有问题,经检查发现时表名称没有加"[]",原始信息如下: SELECT UserName,UserPasswd,SiteUrl  FROM  Work  Where UserName='2012' 修改后信息如下: SELECT UserName,UserPasswd,SiteUrl  FROM  [Work]  Where UserName='2012'

access2003 sql insert into语句报语法错误

我在用c#写access2003 sql insert into时碰到了语句报语法错误的报错. 修改成这样的代码就可以执行了 OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\SmartHome\\Desktop\\data.mdb"); OleDbCommand cmd = new OleDbCommand(); cmd.Connecti

在 System.Data.OleDb.OleDbException 中第一次偶然出现的“System.Data.dll”类型的异常

C#使用OleDB操作ACCESS插入数据时提示: 这个错误是在执行: int rlt = updateCmd.ExecuteNonQuery(); 时产生的,但这个信息是在控制台中输出的,而抛出的异常信息通常是:标准表达式中数据类型不匹配. 出现这个问题的原因是Insert Into语句中为字段指定的值的类型不匹配.

Insert Into 语句的语法错误

错误示意: 一开始程序是: 改正: 一条很简单的插入语句竟然会报错,然而直接在数据库的查询中执行中却没有问题,这个问题困扰了不少时间. 数据库使用的是ACCESS,INSERT INTO语句为insert into userInfo(user, pwd) values('test','test') 一般可能列名发生关键字冲突,于是给user, pwd加了[],即insert into userInfo([user], [pwd]) values('test','test'),就正常了. 原文地址

问题.NETSystem.Data.OleDb.OleDbException 操作必须使用一个可更新的查询

问题:System.Data.OleDb.OleDbException 操作必须使用一个可更新的查询 问题现象:用asp.net连access数据库,查询可以,插入数据报错.在.NET中F5可以使用方法插入数据,但是复制到发布目录下,报错. System.Data.OleDb.OleDbException 操作必须使用一个可更新的查询 问题原因:最简单的原因就是你数据库放置的位置权限不够.一般都是查询可以执行,而要进行写操作就不行了!那是因为保存数据库的磁盘系统是ntfs格式的,文件夹的共享要设

System.Data.SqlClient.SqlError: FILESTREAM 功能被禁用&rdquo;的错误

还原sql2008数据库时遇到"System.Data.SqlClient.SqlError: FILESTREAM 功能被禁用"的错误,在网上搜索解决方案如下: 1.在"开始"菜单中,依次指向"所有程序"."Microsoft SQL Server 2008"和"配置工具",然后单击"SQL Server 配置管理器". 2.在服务列表中,右键单击"SQL Server 服务

System.Data.Oracleclient需要Oracle客户端软件Version8.1.7或更高版本问题

C#连接ORACLE报System.Data.Oracleclient需要Oracle客户端软件Version8.1.7或更高版本问题: 开始Webservice在32位系统ORACLE10g库中web.config中连接串为: <add name="EMRConnectionString" connectionString="Data Source=TEST1;User ID=TEST1;Password=TEST1;Unicode=True" provid

vs2015连接oracle 11g(.net自带方式 using System.Data.OracleClient;)

1,添加引用 System.Data.OracleClient 2,连接语句 string connectionString; string queryString; connectionString = "Data Source=127.0.0.1/orcl;User ID=scott;PassWord=TIGER"; queryString = "SELECT * FROM TEST"; OracleConnection myConnection = new O

(C# Debug)A first chance exception of type &#39;System.ArgumentException&#39; occurred in System.Data.dll

Debug 模式下运行程序的时候,Output 窗口出来个错误“A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll”. 但是并没有直接throw错误.无法知道具体在哪一步发生了这个错误. 如果想知道具体的内容,需要enable 这个debug If you do want to know, in Visual Studio -> Debug (main menu) ->