日期:2014-05-18  浏览次数:20981 次

dataadapter.update()中使用事务的问题?
private void initTable() //初始化表格!
  {
  OracleConnection oracn = new OracleConnection(_connString);
  orada = new OracleDataAdapter("select assemble_id,class_id,class_pid,memo,autoid from element_assemble_child where 1=2", oracn);
  oracb = new OracleCommandBuilder(orada);
  orada.Fill(ds);
  this.dataGridView1.DataSource = ds.Tables[0];
  }

private void Save() //保存表格
  {
  OracleConnection oracn =new OracleConnection(_connString);
  OracleCommand oracmd = new OracleCommand();
  oracmd.Connection = oracn;
  oracn.Open();
  OracleTransaction oratran = oracn.BeginTransaction();
  oracmd.Transaction = oratran;
  orada.UpdateCommand.Transaction = oratran; orada.InsertCommand.Transaction = oratran;
  orada.DeleteCommand.Transaction = oratran;
  try

  {
   
  oracmd.CommandText = "insert into element_assemble(assemble_id,assemble_name,memo) values('" + tbx_id.Text + "','" + tbx_name.Text + "'" +
  ",'" + memo.Text + "')";
   
  oracmd.ExecuteNonQuery();  
   
  orada.Update(ds.Tables[0]);
  oratran.Commit();
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  oratran.Rollback();
  }
}

为什么会报错呢?在红色字体处报错!提示没有将对象实例化!
难道updatecommand还要手工指定吗?
在红色字体上方添加orada.UpdateCommand = oracb.GetUpdateCommand();
  orada.InsertCommand = oracb.GetInsertCommand();
  orada.DeleteCommand = oracb.GetInsertCommand();也不行!

为什么?



------解决方案--------------------
OracleDataAdapter orada = new OracleDataAdapter("select assemble_id,class_id,class_pid,memo,autoid from element_assemble_child where 1=2", oracn); 

------解决方案--------------------
C# code

OracleConnection oracn = new OracleConnection(_connString);
            OracleCommand oracmd = new OracleCommand();
            oracmd.Connection = oracn;
            oracn.Open();
            OracleTransaction oratran = oracn.BeginTransaction();
            oracmd.Transaction = oratran;
            orada.Connection = oracn;
            orada.Command = oracmd;
            //orada.UpdateCommand.Transaction = oratran;            orada.InsertCommand.Transaction = oratran; 
            //orada.DeleteCommand.Transaction = oratran; 

            try
            {

                oracmd.CommandText = "insert into element_assemble(assemble_id,assemble_name,memo) values('" + tbx_id.Text + "','" + tbx_name.Text + "'" +
                    ",'" + memo.Text + "')";

                //这个地方怎么进行了两次插入操作????
                oracmd.ExecuteNonQuery();

                orada.Update(ds.Tables[0]);
                oratran.Commit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                oratran.Rollback();
            }