access很郁闷的
注意设置一下数据库的字段

解决方案 »

  1.   

    以下是向数据库中写入数据的代码,请各位帮帮忙啊using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace test
    {
    /// <summary>
    /// AddQuestion 的摘要说明。
    /// </summary>
    public class AddQuestion : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox TextBox3;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.TextBox TextBox4;
    protected System.Web.UI.WebControls.TextBox TextBox5;
    protected System.Web.UI.WebControls.TextBox TextBox6;
    protected System.Web.UI.WebControls.TextBox TextBox7;
    protected System.Web.UI.WebControls.TextBox TextBox8;
    protected System.Web.UI.WebControls.TextBox TextBox9;
    protected System.Web.UI.WebControls.TextBox TextBox10;
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.TextBox TextBox2;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    }
    private string check(string v)
    {
    v = v.Replace("'","");
    v = Server.HtmlEncode(v);
    return v;
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;";
    string DataBase  = Server.MapPath("exam.mdb");
    strConnection += "Data Source=" + DataBase;
    string strSQL = "SELECT * FROM problem where title='" +TextBox1.Text + "'";
    OleDbConnection objConnection = new OleDbConnection(strConnection);
    OleDbCommand objCommand =new OleDbCommand(strSQL, objConnection);
    OleDbDataReader objDataReader ;
    objCommand.Connection.Open();
    objDataReader = objCommand.ExecuteReader();
    if (objDataReader.Read() == true) 
    {
    Label1.Text = "对不起,问题已存在!";
    return;}
    objCommand.Connection.Close();
    string sql = "insert into problem (title,type,ans1,ans2,ans3,ans4,ans5,key_,pub,nandu)values ('" + check(TextBox1.Text) + "','"+ check(TextBox3.Text) + "','"+check(TextBox4.Text)+"','"+check(TextBox5.Text)+"','"+check(TextBox6.Text)+"','"+check(TextBox7.Text)+"','"+check(TextBox8.Text)+"','"+check(TextBox9.Text)+"','"+check(TextBox10.Text)+"','"+check(TextBox2.Text)+"')";
    OleDbCommand addCommand =new OleDbCommand(sql, objConnection);
    addCommand.Connection.Open();
    addCommand.ExecuteNonQuery();
    addCommand.Connection.Close(); Response.Write ("<script>alert(\"添加成功!\")</script>");return;




    }
    }
    }
      

  2.   

    如果代码没出问题的话
    肯定是access数据设置字段的问题了 
    仔细检查以下》。。
      

  3.   

    -_-b
    看不出哪错了。提示你一点,就是access中是int类型的数据,在代码中就不用加''了。