当前位置:编程学习 > C#/ASP.NET >>

asp.net 连接access数据库出现insert into 语法错误

以下是具体代码(asp.net3.5,access2003)

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Configuration;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button_register_Click(object sender, EventArgs e)
    {
        String username = user_name.Text;
        String email_ = email.Text;
        String pass = password.Text;
        String realname = real_name.Text;
        String 易做图_ = 易做图.Text;
        String qq_ = qq.Text;
        String address_ = address.Text;
        String website_ = website.Text;
        String tel = telphone.Text;
        String productid = product_id.Text;
        String remark_ = remark.Text;
        string sql = "INSERT INTO register (user_name,email,password,real_name,易做图,qq,address,website,telphone,product_id,remark) values ( '" + username + "','"
            + email_ + "','" + pass + "','" + realname + "','" + 易做图_ + "','" + qq_ + "','" + address_ + "','" + website_ + "','" + tel + "','" + productid + "','" + remark_ + "')";

        OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["mydb"].ToString());
        OleDbCommand comm = new OleDbCommand(sql,conn);
        comm.Connection.Open();
        comm.ExecuteNonQuery();
        comm.Connection.Close();       

      

 
    }

}

 

 

------------------------错误信息----------------------------------

INSERT INTO 语句的语法错误。

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

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

源错误: 

行 37: OleDbCommand comm = new OleDbCommand(sql,conn); 行 38: comm.Connection.Open(); 行 39: comm.ExecuteNonQuery(); 行 40: 行 41:

源文件: d:\kiyo\register.aspx.cs    行: 39 

堆栈跟踪: 

[OleDbException (0x80040e14): INSERT INTO 语句的语法错误。] System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) +1006560 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +255 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +188 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +58 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +161 System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +113 _Default.Button_register_Click(Object sender, EventArgs e) in d:\kiyo\register.aspx.cs:39 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

追问:恩,谢谢!!!改了以后,没有提示insert into的错误了。。。但是提示“查询值的数目与目标字段中的数目不同”,是因为我的数据表里id一项是自动生成的,没有填写进来是吗?那要怎么改呢。。。哦,还有,为什么pass要加上【】号呢?

答案:
您好,我不建议您拼接字符串,拼接字符串很容易遭到SQL攻击,同时也极容易发生错误(拼接错误)。
请改成以下模式(参数化模式)


 string sql = "INSERT INTO register (user_name,email,[password],real_name,易做图,qq,address,website,telphone,product_id,remark) values(@user_name,@email,@password,@real_name,@易做图,@address,@website,@telephone,@product_id,@remoark)";


        OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["mydb"].ToString());


        OleDbCommand comm = new OleDbCommand(sql,conn);


comm.Parameters.AddWithValue("@user_name",你的真实内容);
comm.Parameters.AddWithValue("@email",你的真实内容);
comm.Parameters.AddWithValue("@password",你的真实内容);
comm.Parameters.AddWithValue("@real_name",你的真实内容);
comm.Parameters.AddWithValue("@易做图",你的真实内容);
comm.Parameters.AddWithValue("@address",你的真实内容);
comm.Parameters.AddWithValue("@website",你的真实内容);
comm.Parameters.AddWithValue("@telephone",你的真实内容);
comm.Parameters.AddWithValue("@product_id",你的真实内容);
comm.Parameters.AddWithValue("@remoark",你的真实内容);

        comm.Connection.Open();
        comm.ExecuteNonQuery();
        comm.Connection.Close();      

上一个:asp分页,请高手求救
下一个:帮忙看一下这段asp代码错在那里

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,