string source = "pcdb.mdb";
            string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + source;
            string sql = "insert into [jswg] (完工日期,工号,姓名,工序编号,工序名称,工作时间,备注) values (@wgrq,@gh,@xm,@gxbh,@gxmc,@gzsj,@bz)";
 OleDbConnection olecon = new OleDbConnection(conn);
            olecon.Open();
            OleDbCommand olecmd = new OleDbCommand(sql, olecon);
olecmd.Parameters.Add("@wgrq", OleDbType.DBDate, 50).Value = dateTimePicker1.Text;
            olecmd.Parameters.Add("@gh", OleDbType.VarChar, 50).Value = this.textBox7.Text;
            olecmd.Parameters.Add("@xm", OleDbType.VarChar, 50).Value = this.textBox1.Text;
            olecmd.Parameters.Add("@gxbm", OleDbType.VarChar, 50).Value = this.textBox2.Text;
            olecmd.Parameters.Add("@gxmc", OleDbType.VarChar, 50).Value = this.comboBox1.Text;
            olecmd.Parameters.Add("@gzsj", OleDbType.Integer, 50).Value = Convert.ToInt32(this.textBox3.Text);
            olecmd.Parameters.Add("@bz", OleDbType.VarChar, 50).Value = this.textBox5.Text;
            int i = Convert.ToInt32(olecmd.ExecuteNonQuery());
            if (i > 0)
            {
                MessageBox.Show("插入成功! ", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                MessageBox.Show("发现异常!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            olecon.Close();跟踪了下,i=1,但是往数据表中没有记录,但是debug下的数据表中却有记录,怪事!请问各位该怎么办?

解决方案 »

  1.   

    olecmd.Parameters.Add("@gxbm", OleDbType.VarChar, 50).Value = this.textBox2.Text;
    ==
    gxbm?笔误吧
      

  2.   

    还有Access中只认参数的顺序,不认参数的名字,参数可以用占位符 ? 来代替
      

  3.   

    以前我做webform的时候插入数据都这么插的,也都插入了!
      

  4.   

    问题是debug下面的数据库里已经插入了,但是项目里的数据库却没插入,奇哉怪也!
      

  5.   


    public partial class Form1 : Form
        {
    Form1数据成员#region Form1数据成员
            private DataTable DT = new DataTable();
            private SqlDataAdapter SDA = new SqlDataAdapter();
    #endregionForm1构造函数#region  Form1构造函数 
            public Form1()
            {
                InitializeComponent();
            }
    #endregion连接数据库显示数据#region  连接数据库显示数据
            private void Form1_Load(object sender, EventArgs e)
            {            SqlConnection conn = new SqlConnection("server=127.0.0.1;database=pubs;uid=sa");
                SqlCommand SCD = new SqlCommand("select * from tables", conn);
                SDA.SelectCommand = SCD;
                SDA.Fill(DT);
                dataGridView1.DataSource = DT;
            }
    #endregion使用Update更新数据库#region  使用Update更新数据库
            private void toolStripButton1_Click(object sender, EventArgs e)
            {
                try
                {
                    SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);                
                    SDA.Update(DT);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
                MessageBox.Show("更新成功!");
            }
    #endregion