"INSERT INTO 语句的语法错误" 急呀!!

powre 2009-10-05 04:36:47
本人在做一个小型的企业网,用的是access的数据库.在做添加产品页面时,运行时提示是:

"INSERT INTO 语句的语法错误"
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误。

源错误:
行 141: cmd2.ExecuteNonQuery();


本人向来比较粗心,因此查了一天也没查出个所以然.希望各位不吝赐教!

以下为粘贴源代码:


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;

public partial class Admin_Product_Add : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Manager"] == null)
{
Response.Write("<script language=javascript>alert('请先登录!');location.href='login.aspx';</script>");

}

}

protected void Btn_Submit_Click(object sender, EventArgs e)
{
if ((tb_ProductName.Text == "") || (dr_ProductType.Text == "==请选择==") || (tb_kuanshi.Text == "") || (tb_xiulong.Text == "") || (tb_photo.Text == "") || (tb_mianliao.Text == "") || (tb_ling.Text == ""))
{
Response.Write("<script language=javascript>alert('请添加完整各选项!')</script>");
}

else
{

string strName = this.tb_ProductName.Text;
string strType = this.dr_ProductType.Text;
string strPingPai = this.tb_PingPai.Text;
string strKuanshi = this.tb_kuanshi.Text;
string strXiuLong = this.tb_xiulong.Text;
string strPhoto = this.tb_photo.Text;
string strMianliao = this.tb_mianliao.Text;
string strStyle = this.tb_style.Text;
string strSeason = this.tb_season.Text;
string strLing = this.tb_ling.Text;

if (this.smallPhoto.HasFile)
{
//设置小图上传
HttpPostedFile postFile = this.smallPhoto.PostedFile;
string smallname = smallPhoto.FileName.ToString();
string type1 = smallPhoto.PostedFile.ContentType.ToString();
string type2 = smallname.Substring(smallname.LastIndexOf(".") + 1);
string strFileName = smallname;
string size = smallPhoto.PostedFile.ContentLength.ToString();
string timefile = DateTime.Now.ToString("yyMMddHHmmss");
string smallSavePath = Server.MapPath("~\\Products\\" + timefile + "small" + smallname);
if (type2 == "gif" || type2 == "jpg" || type2 == "bmp" || type2 == "png")
{

postFile.SaveAs(smallSavePath);

}
else
{
Response.Write("<script >alert('您上传的文件格式不对!');location.href='Product_Add.aspx';</script>");

}

//设置大图上传格式
HttpPostedFile postFile2 = this.bigPhoto.PostedFile;
string bigname = bigPhoto.FileName.ToString();
string type3 = bigname.Substring(bigname.LastIndexOf(".") + 1);
string strFileName2 = bigname;
string size2 = bigPhoto.PostedFile.ContentLength.ToString();
string timefile2 = DateTime.Now.ToString("yyMMddHHmmss");
string bigSavePath = Server.MapPath("~\\Products\\" + timefile + "big" + bigname);
if (type3 == "gif" || type3 == "jpg" || type3 == "bmp" || type3 == "png")
{
postFile.SaveAs(bigSavePath);
}
else
{
Response.Write("<script language=javascript>alert('您上传的文件格式不对!')</script>");
}

string strLoad = Server.MapPath("~//data//clothes.mdb"); //获取指定数据库文件的相对路径
OleDbConnection conn = new OleDbConnection("provider=Microsoft.Jet.OleDb.4.0;Data Source=" + strLoad + ";");

conn.Open();

string str = " Select count(*) from Product where product_name='" + tb_ProductName.Text.ToString() + "'";
OleDbCommand cmd = new OleDbCommand(str, conn);
cmd.Connection = conn;
int intcont = Convert.ToInt32(cmd.ExecuteScalar());
if (intcont > 0)
{
Response.Write("<script language=javascript>alert('对不起,不允许填写相同记录!');location.href='Product_Add.aspx';</script>");
}
else
{
try
{
string InsertSql = "Insert into Product(Product_Name,Class_Name,PingPai,Kuanshi,XiuLong,photo,mianliao,style,season,ling,size,smallImg,price,bigImg,Product_time)Values(@Product_Name,@Class_Name,@PingPai,@Kuanshi,@XiuLong,@photo,@mianliao,@style,@season,@ling,@size,@smallImg,@price,@bigImg,@Product_time)";
OleDbCommand cmd2 = new OleDbCommand(InsertSql, conn);

OleDbParameter[] oledbParams = new OleDbParameter[15];

OleDbParameter paramName=(new OleDbParameter("@Product_Name", OleDbType.VarChar, 20));
OleDbParameter paramClass = (new OleDbParameter("@Class_Name", OleDbType.VarChar, 50));
OleDbParameter paramPingPai=(new OleDbParameter("@PingPai", OleDbType.VarChar, 20));
OleDbParameter paramKuanshi=(new OleDbParameter("@Kuanshi", OleDbType.VarChar, 20));
OleDbParameter paramXiuLong=(new OleDbParameter("@XiuLong", OleDbType.VarChar, 20));
OleDbParameter paramPhoto=(new OleDbParameter("@photo", OleDbType.VarChar, 20));
OleDbParameter paramMianliao=(new OleDbParameter("@mianliao", OleDbType.VarChar, 20));
OleDbParameter paramStyle=(new OleDbParameter("@style", OleDbType.VarChar, 20));
OleDbParameter paramSeason = (new OleDbParameter("@season", OleDbType.VarChar, 20));
OleDbParameter paramLing = (new OleDbParameter("@ling", OleDbType.VarChar, 20));
OleDbParameter paramSize=(new OleDbParameter("@size", OleDbType.VarChar, 20));
OleDbParameter paramSmallImg=(new OleDbParameter("smallImg", OleDbType.VarChar, 50));
OleDbParameter paramPrice=(new OleDbParameter("price", OleDbType.Currency,10));
OleDbParameter paramBigimg=(new OleDbParameter("bigImg", OleDbType.VarChar, 50));
OleDbParameter paramProduct_time=(new OleDbParameter("Product_time", OleDbType.DBDate));

paramName.Value=tb_ProductName.Text;
paramClass.Value = dr_ProductType.SelectedItem.Text;
paramPingPai.Value =tb_PingPai.Text;
paramKuanshi.Value =tb_kuanshi.Text;
paramXiuLong.Value = tb_xiulong.Text;
paramPhoto.Value = tb_photo.Text;
paramMianliao.Value = tb_mianliao.Text;
paramStyle.Value = tb_style.Text;
paramSeason.Value= tb_season.Text;
paramLing.Value = tb_size.Text;
paramSize.Value = tb_size.Text;
paramSmallImg.Value = smallSavePath;
paramPrice.Value = Convert.ToDecimal(Product_price.Text);
paramBigimg.Value = bigSavePath;
paramProduct_time.Value = Convert.ToDateTime(Convert.ToDecimal((sale_time.Text).ToString()));
141行位置: cmd2.ExecuteNonQuery();
GridView1.DataSourceID = AccessDataSource1.ID;
conn.Close();
Response.Write("<script language=javascript>alert('添加记录成功!');</script>");
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());

}
}
}
else
{
Response.Write("<script>alert('请上传图片文件!');location.href='Product_Add.aspx';</script>");

}



}
}
}
...全文
392 40 打赏 收藏 转发到动态 举报
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
paramProduct_time.Value = Convert.ToDateTime(sale_time.Text.ToString());


如果值中有单引号,那么

str=str.Replace("'","''");

我觉得你的问题加断点,完全可以看出来,还有,把SQL语句复制到 查询分析器里面去试试---
  • 打赏
  • 举报
回复
另外我给你一个以后开发的建议:使用Sql Server Compact而不要使用Jet数据库。SSCE具有Jet数据库的一切好处:数据库文件是单个文件、DLL即可驱动、快速。而且它跟SQL Server的语言完全一样,管理工具也丰富。

http://technet.microsoft.com/zh-cn/library/ms173037(SQL.90).aspx

对于编程人员,使用Linq to SQL操作数据库可以避免纠缠于SQL语法。Linq to SQL同时支持Sql Server和Sql Server Compact,只要在数据库连接字符串中直接给出.sdf文件的路径就可以,而不用你修改任何一行程序代码。
  • 打赏
  • 举报
回复
看一下你在问题中贴的代码,惨不忍睹:

1. oledbParams 这个变量有什么用?哪里使用它了?

2. paramName这个变量以及其它一堆类似的DBParameter又有什么用?你只是new一个它的对象,然后给这个对象赋值,可是对象有什么用?跟cmd2有个屁关系?

这样的代码纯粹过分抄袭,而没有理解变量之间的必须的联系,虽然看似蒸汽,逻辑实在是惨不忍睹。
ynduanlian 2009-10-06
  • 打赏
  • 举报
回复
Insert into Product(Product_Name,Class_Name,PingPai,Kuanshi,XiuLong,photo,mianliao,style,season,ling,[size],smallImg,price,bigImg,Product_time) values

我也碰到了Insert Into语句错误,结果如上所述,是用到了Access的何留字没有用方括号括起来
ynduanlian 2009-10-06
  • 打赏
  • 举报
回复
Insert into Product(Product_Name,Class_Name,PingPai,Kuanshi,XiuLong,photo,mianliao,style,season,ling,[size],smallImg,price,bigImg,Product_time) values

我也碰到了Insert Into语句错误,结果如上所述,是用到了Access的何留字没有用方括号括起来
powre 2009-10-06
  • 打赏
  • 举报
回复
我又试了一下,发觉在Access中用参数的方式应该是不行的,于是我用加载控件变量的方式来做,错误如下:

--------------------------------------------------------------------------------

INSERT INTO 语句的语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误。

源错误:


行 106: string InsertSql = "insert into product(Product_Name,Class_Name, PingPai,kuanshi,XiuLong,photo,mianliao,style,season,ling,size,smallImg,price,bigImg,product_time) Values('" + tb_ProductName.Text + "','" + dr_Product_Type.Text + "','" + tb_PingPai.Text + "','"+strKuanshi+"','"+strXiuLong+"','"+strPhoto+"','"+strMianliao+"','"+strStyle+"','"+strSeason+"','"+strLing+"','"+strSize+"','"+smallSavePath+"','"+money_Price+"','"+bigSavePath+"','"+saletime+"')";
行 107: OleDbCommand cmd2 = new OleDbCommand(InsertSql, conn);
行 108: cmd2.ExecuteNonQuery();
行 109: conn.Close();
行 110: Response.Write("<script language=javascript>alert('添加记录成功!');location.href='add_Clothes.aspx';</script>");


源文件: d:\My Documents\Visual Studio 2005\WebSites\服装\Admin\Add_Clothes.aspx.cs 行: 108

堆栈跟踪:


[OleDbException (0x80040e14): INSERT INTO 语句的语法错误。]
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) +65
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +181
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +307
System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +77
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +188
System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +112
Admin_Add_Clothes.Button1_Click(Object sender, EventArgs e) in d:\My Documents\Visual Studio 2005\WebSites\服装\Admin\Add_Clothes.aspx.cs:108
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +96
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +116
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3825



我写的代码如下(这次可能会好查一点吧,拜托各位了!):
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class Admin_Add_Clothes : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AccessDataSource1.SelectCommand = "Select * from product";
GridView1.DataSourceID = AccessDataSource1.ID;
GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
if ((tb_ProductName.Text == "") || (dr_Product_Type.SelectedItem.Text == "==请选择==") || (tb_kuanshi.Text == "") || (tb_XiuLong.Text == "") || (tb_photo.Text == "") || (tb_mianliao.Text == "") || (tb_ling.Text == ""))
{
Response.Write("<script language=javascript>alert('请添加完整各选项!')</script>");
}

else
{

string strName = tb_ProductName.Text;
string strType = dr_Product_Type.SelectedItem.Text;
string strPingPai = tb_PingPai.Text;
string strKuanshi = tb_kuanshi.Text;
string strXiuLong = tb_XiuLong.Text;
string strPhoto = tb_photo.Text;
string strMianliao = tb_mianliao.Text;
string strStyle = tb_style.Text;
string strSeason = tb_season.Text;
string strLing = tb_ling.Text;
string strSize = tb_size.Text;
decimal money_Price = Convert.ToDecimal(product_price.Text);
DateTime saletime = Convert.ToDateTime(sale_Date.Text);


if (this.smallphoto.HasFile)
{
//设置小图上传
HttpPostedFile postFile = this.smallphoto.PostedFile;
string smallname = smallphoto.FileName.ToString();
string type1 = smallphoto.PostedFile.ContentType.ToString();
string type2 = smallname.Substring(smallname.LastIndexOf(".") + 1);
string strFileName = smallname;
string size = smallphoto.PostedFile.ContentLength.ToString();
string timefile = DateTime.Now.ToString("yyMMddHHmmss");
string smallSavePath = Server.MapPath("~\\Products\\" + timefile + "small" + smallname);
if (type2 == "gif" || type2 == "jpg" || type2 == "bmp" || type2 == "png")
{
// Response.Write(Server.MapPath("~/Products"));
postFile.SaveAs(smallSavePath);

}
else
{
Response.Write("<script >alert('您上传的文件格式不对!');location.href='Product_Add.aspx';</script>");
// Response.Redirect(Request.CurrentExecutionFilePath);
}

//设置大图上传格式
HttpPostedFile postFile2 = this.bigphoto.PostedFile;
string bigname = bigphoto.FileName.ToString();
string type3 = bigname.Substring(bigname.LastIndexOf(".") + 1);
string strFileName2 = bigname;
string size2 = bigphoto.PostedFile.ContentLength.ToString();
string timefile2 = DateTime.Now.ToString("yyMMddHHmmss");
string bigSavePath = Server.MapPath("~\\Products\\" + timefile + "big" + bigname);
if (type3 == "gif" || type3 == "jpg" || type3 == "bmp" || type3 == "png")
{
postFile.SaveAs(bigSavePath);
// Response.Write("<script>alert('图片上传成功!')</script>");
}
else
{
Response.Write("<script language=javascript>alert('您上传的文件格式不对!')</script>");
}


string strLoad = Server.MapPath("~//data//clothes.mdb"); //获取指定数据库文件的相对路径

//确定名称是否惟一
OleDbConnection conn = new OleDbConnection("provider=Microsoft.Jet.OleDb.4.0;Data Source=" + strLoad + ";");
conn.Open();
string str = " Select count(*) from Product where product_name='" + tb_ProductName.Text.ToString() + "'";
OleDbCommand cmd = new OleDbCommand(str, conn);
cmd.Connection = conn;
int intcont = Convert.ToInt32(cmd.ExecuteScalar());
if (intcont > 0)
{
Response.Write("<script language=javascript>alert('对不起,不允许填写相同的服装名称!');location.href='Product_Add.aspx';</script>");
}
else
{
try
{
string InsertSql = "insert into product(Product_Name,Class_Name, PingPai,kuanshi,XiuLong,photo,mianliao,style,season,ling,size,smallImg,price,bigImg,product_time) Values('" + tb_ProductName.Text + "','" + dr_Product_Type.Text + "','" + tb_PingPai.Text + "','"+strKuanshi+"','"+strXiuLong+"','"+strPhoto+"','"+strMianliao+"','"+strStyle+"','"+strSeason+"','"+strLing+"','"+strSize+"','"+smallSavePath+"','"+money_Price+"','"+bigSavePath+"','"+saletime+"')";
OleDbCommand cmd2 = new OleDbCommand(InsertSql, conn);
cmd2.ExecuteNonQuery();
conn.Close();
Response.Write("<script language=javascript>alert('添加记录成功!');location.href='add_Clothes.aspx';</script>");

}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());

}
}
}
else
{
Response.Write("<script>alert('请上传图片文件!');location.href='Add_Clothes.aspx';</script>");

}
}
}


powre 2009-10-06
  • 打赏
  • 举报
回复
问题解决了,真的是保留字的问题。真的要向各位老大学习呀,希望以后多多支持,散分!!
qinyangqin123 2009-10-06
  • 打赏
  • 举报
回复
E4TERTGWSWERT
IT山IT水 2009-10-06
  • 打赏
  • 举报
回复
帮顶!
tianyu_lianxin 2009-10-06
  • 打赏
  • 举报
回复
你先在ACCESS数据库中,将那条INSERT 语句执行下,其中的变量替换成实际要输入的值,
如果可以执行那你的INSERT 语句应该没错,错的应该就是在数据的转换方面出的错,
再考虑数据转换方面的问题.
如果没才错的话,这条语句替换参数后在ACCESS中也是不能执行的,
因为ACCESS中有保留关键字,详细情况可以去查下,在个就是你数据转换的问题,这方面不很在行不敢乱说.
powre 2009-10-05
  • 打赏
  • 举报
回复
数据库连接没有问题
以下是错误提示:

INSERT INTO 语句的语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误。

源错误:


行 139: paramBigimg.Value = bigSavePath;
行 140: paramProduct_time.Value = Convert.ToDateTime((sale_time.Text).ToString());
行 141: cmd2.ExecuteNonQuery();
行 142: GridView1.DataSourceID = AccessDataSource1.ID;
行 143: conn.Close();


源文件: d:\My Documents\Visual Studio 2005\WebSites\服装\Admin\Product_Add.aspx.cs 行: 141

堆栈跟踪:


[OleDbException (0x80040e14): INSERT INTO 语句的语法错误。]
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +177
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +194
System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +56
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +105
System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +88
Admin_Product_Add.Btn_Submit_Click(Object sender, EventArgs e) in d:\My Documents\Visual Studio 2005\WebSites\服装\Admin\Product_Add.aspx.cs:141
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +97
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4886



春天的气息 2009-10-05
  • 打赏
  • 举报
回复
代码太长了,没看,不过你可以输出一下错误的提示信息哦!
李小冲 2009-10-05
  • 打赏
  • 举报
回复
截个断点查看一下啊,一般什么user,size这样的表和字段名,加上方括号[],注意一下数据类型是否一致,这样基本可以排除一些错误了。
dingpo2099 2009-10-05
  • 打赏
  • 举报
回复
顶一下~!
24K純帥 2009-10-05
  • 打赏
  • 举报
回复
是不是连接数据库有问题啊,代码明天再看,要断网了
powre 2009-10-05
  • 打赏
  • 举报
回复
应该不是楼上说的这个问题
啊兵 2009-10-05
  • 打赏
  • 举报
回复
楼主有些关键字你是不能直接使用的,最好把字段加上【】吧
powre 2009-10-05
  • 打赏
  • 举报
回复
这句有错误:
paramPrice.Value = Convert.ToDecimal(Product_price.Text);

我是要把字符型数值转换为货币型,请问怎么修改?
xray2005 2009-10-05
  • 打赏
  • 举报
回复
你DEBUG以下,把错误的堆栈信息贴出来。再把你跟踪到的SQL语句贴出来看看
powre 2009-10-05
  • 打赏
  • 举报
回复
或者谁有Access数据库 添加记录的代码,贴一段出来让我参考一下,谢谢!!
加载更多回复(19)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧