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

dataadapter如何用update方法做表间数据备份
表t1,t2结构相同,用如下方法从t1表取数据并插入t2表,系统无任何出错提示,但是t2表无任何插入数据,代码如下:
OracleConnection oconn = new OracleConnection(GetConnString());
OracleDataAdapter oda = new OracleDataAdapter("select * from t1", oconn);
DataTable dt1 = new DataTable();
oda.Fill(dt1);
//以上取得t1表数据填充dt1
//以下用dt1更新t2表
OracleConnection oconn1 = new OracleConnection(GetConnString());
OracleDataAdapter oda1 = new OracleDataAdapter("select * from t2", oconn1);
OracleCommandBuilder ocb1 = new OracleCommandBuilder(oda1);
oconn1.Open();
oda1.Update(dt1);
dt1.AcceptChanges();
oconn1.Close();
问题不知出在那里?

------解决方案--------------------
SQL code

OracleConnection oconn = new OracleConnection(GetConnString()); 
OracleDataAdapter oda = new OracleDataAdapter("select * from t1", oconn); 
DataTable dt1 = new DataTable(); 
oda.Fill(dt1); 

OracleConnection oconn1 = new OracleConnection(GetConnString()); 
oconn1.Open(); 
OracleDataAdapter oda1 = new OracleDataAdapter("select * from t2", oconn1); 
DataTable dt2 = new DataTable(); 
oda1.Fill(dt2); 

--更新資料
foreach( DataRow row in dt1.Rows)
 {
  try
 { 
   //import row :                  
 object [] row2 = row.ItemArray;
 dt2.BeginLoadData();
 dt2.LoadDataRow(row2, false);
 dt2.EndLoadData();
 }
 catch(Exception ex)
 {
   throw ex;
 }
}

OracleCommandBuilder ocb1 = new OracleCommandBuilder(oda1); 
oda1.Update(dt2); 
dt2.AcceptChanges(); 
oconn1.Close();