首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

BatchExecNonQuery解决方法

2012-04-28 
BatchExecNonQuerypublic static bool BatchExecNonQuery(string connString, string[] batchCmdText, boo

BatchExecNonQuery
public static bool BatchExecNonQuery(string connString, string[] batchCmdText, bool rollBackFlag)
{
  bool flag;
  if ((batchCmdText == null) || (batchCmdText.Length == 0))
  {
  return true;
  }
  int index = 0;
  OracleConnection conn = null;
  OracleTransaction trans = null;
  try
  {
  conn = OpenConn(connString);
  trans = rollBackFlag ? conn.BeginTransaction() : null;
  OracleCommand cmd = new OracleCommand();
  index = 0;
  while ((index < batchCmdText.Length) && (batchCmdText[index] != null))
  {
  PrepareCommand(cmd, conn, trans, CommandType.Text, batchCmdText[index], null);
  cmd.ExecuteNonQuery();
  index++;
  }
  if (trans != null)
  {
  trans.Commit();
  }
  flag = true;
  }
  catch (Exception exception)
  {
  if (trans != null)
  {
  trans.Rollback();
  }
  throw new Exception("批处理出错!:" + batchCmdText[index] + "--" + exception.ToString());
  }
  return flag;
}

 

 


[解决办法]
不知道你的问题在哪里
但是你的代码有问题
数据库资源未释放

C# code
 public static bool BatchExecNonQuery(string connString, string[] batchCmdText, bool rollBackFlag)    {        bool flag;        if ((batchCmdText == null) || (batchCmdText.Length == 0))        {            return true;        }        int index = 0;        OracleConnection conn = null;        OracleTransaction trans = null;        try        {            conn = OpenConn(connString);            trans = rollBackFlag ? conn.BeginTransaction() : null;            OracleCommand cmd = new OracleCommand();            index = 0;            while ((index < batchCmdText.Length) && (batchCmdText[index] != null))            {                PrepareCommand(cmd, conn, trans, CommandType.Text, batchCmdText[index], null);                cmd.ExecuteNonQuery();                index++;            }            if (trans != null)            {                trans.Commit();            }            flag = true;        }        catch (Exception exception)        {            if (trans != null)            {                trans.Rollback();            }            throw new Exception("批处理出错!:" + batchCmdText[index] + "--" + exception.ToString());        }        finally        {            //注意回收数据库链接资源            if (conn != null && conn.State != ConnectionState.Closed)            {                conn.Close();                conn.Dispose();                conn = null;            }        }        return flag;    } 

热点排行