//事先生成临时表 create global temporary table temp_20061030 (id int,name varchar2(256)) on commit delete rows;
OracleDBOperation dbo = null;//OracleDBOperation为对Oracle操作封装的自定义的类。各位可以用Oracle自带的操作类和方法。此处只是说明处理方法。
OracleTransaction trans = null;//关键第一步
try
{
dbo = new OracleDBOperation();//实例化并连接数据库
trans=dbo.Connection.BeginTransaction();//关键第二步
/*--正常利用临时表的业务处理开始--*/
string sql="insert into temp_20061030 (id,name) select t.id,t.name from table1 t where t.id<1000";
dbo.ExecuteStatement(sql);
sql="select max(id) id,count(*) cnt from temp_20061030";
DataTable dt = dbo.GetTable(sql);
if(dt.Rows.Count>0)
{
string id=dt.Rows[0]["id"].ToString();
string cnt=dt.Rows[0]["cnt"].ToString();
MessageBox.Show(id+"/"+cnt);
}
else{
throw new Exception("没有数据");
}
/*--正常利用临时表的业务处理结束--*/
trans.Commit();//关键第三步
}
catch(Exception ex)
{
/*--释放也很重要--*/
if(trans!=null){
trans.Rollback();
}
MessageBox.Show(ex.Message);
}
finally{
/*--释放也很重要--*/
if(trans!=null){
trans.Dispose();
trans=null;
}
if(dbo!=null){
dbo.Close();
dbo.Dispose();
dbo=null;
}
}
临时表在事务内有效时,事务Commit或Rollback都会清空临时表。
本文作者:网友 来源:网络
CIO之家 www.ciozj.com 微信公众号:imciow