我在.aspx页面上插入下面的控件
<span>姓名:</span><asp:textbox ID="TextBox1" runat="server"></asp:textbox>
<span>年龄:</span><asp:textbox ID="TextBox2" runat="server"></asp:textbox>
<span>性别:</span><asp:textbox ID="TextBox3" runat="server"></asp:textbox>
<span>职业:</span><asp:textbox ID="TextBox4" runat="server"></asp:textbox><asp:button ID="Button1" runat="server" text="提交" OnClick="fill" />
目的是往access数据库中插入数据,.aspx.cs页面上的c#代码如下:
using System;
using System.Data;
using System.Data.OleDb;
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;public partial class test : System.Web.UI.Page
{
    static OleDbConnection conn = new OleDbConnection(ConfigurationManager.AppSettings["test_lb"]);
    OleDbCommand cmd1=new OleDbCommand("select name,age,sex,work from test1",conn );
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
        //这一部分是我写的从数据库中读数据的代码,先不用理会
        conn.Open();
        datalist1.DataSource = cmd1.ExecuteReader();
        datalist1.DataBind();
        conn.Close();
        }
    }    protected void fill(object sender, EventArgs e)
    {
        string strName = this.TextBox1.Text;
        string age = TextBox2.Text;
        string sex = TextBox3.Text;
        string work = TextBox4.Text;
        conn.Open();
        OleDbCommand cmd2 = new OleDbCommand("insert into test1(name,sex,age,work) values(@strName,@sex,@age,@work )", conn);
        cmd2.ExecuteNonQuery();
        conn.Close();    }
}
我运行的时候能从数据库读数据,但是输入数据点击提交后页面报错,说“insert into”语句语法错误,而源错误却是下面这种情况,其中红色文字是报错页面标出来的。行 35:         conn.Open();
行 36:         OleDbCommand cmd2 = new OleDbCommand("insert into test1(name,sex,age,work) values(@strName,@sex,@age,@work )", conn);
行 37:         cmd2.ExecuteNonQuery();
行 38:         conn.Close();
请教一下高手是不是我的sql语句写得有问题?多谢了!!
 

解决方案 »

  1.   

    (@strName,@sex,@age,@work )", 这些是哪里来的??
      

  2.   

    insert into [test1](name,sex,age,work) values('"+strName+"','"+sex+"','"+age+"','"+work+"' )这样吧
      

  3.   

    SQL语句改成这样:
    insert into test1(name,sex,age,work) values('"+this.TextBox1.Text+"',"+this.TextBox1.Text+",'"+this.TextBox1.Text+"','"+this.TextBox1.Text+"')
      

  4.   

    我把这句("insert into test1(name,sex,age,work) values(@strName,@sex,@age,@work )", conn);直接换成赋值语句:("insert into test1(name,sex,age,work) values('王末','男','12','程序员')", conn);可是页面还是报那样的错误!!!郁闷阿!
      

  5.   


    换了,还是不行,我觉得好像问题不出在这,页面报错的时候把错误行定到了第37行,是不是这个有问题!!?
    行 35:        conn.Open(); 
    行 36:        OleDbCommand cmd2 = new OleDbCommand("insert into test1(name,sex,age,work) values(@strName,@sex,@age,@work )", conn); 
    行 37:        cmd2.ExecuteNonQuery(); 
    行 38:        conn.Close(); 
      

  6.   

    可能是列名称中有Access保留字
      

  7.   

    如果有保留字,读取时是不会报错的!
    个人感觉不是插入语句有问题,是不是你的Access只读,或者其他的原因。
    如果不对请别见笑!
      

  8.   

    我估计age字段是int型的,而程序中提取的是string型的。
    排错方法:
    一个一个字段insert,到哪个字段出错就是哪的错。
      

  9.   

    现在错误解决了,不过还不知道问题出在哪。我又新建了一个表里面只放了id(自动递增)字段和name字段,控件也只留下了testbox1,sql语句除了参数有变化外其他没变,这次成功了!!然后我又加上了“age”字段,同样也成功了,age字段在表里是int类型的。谢谢各位了!我会把10分分别发给2楼和五楼的,我从他们那又学了点东西,呵呵!!