"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>");
}
}
}
}