/// <summary> /// 执行命令或存储过程 /// </summary> /// <param name="cmd">命令对象</param> /// <returns></returns> public virtual T Execute <T>(OracleCommand cmd) { object retval = default(T); // 返回值 Type type = typeof(T); // 返回值类型 try { // 关联事务,若为空,则不使用事务操作 cmd.Transaction = _Tran; // 根据返回值类型来判断调用哪个方法 if (type == typeof(int)) { // 只返回影响行数 retval = cmd.ExecuteNonQuery(); // retval = cmd.ExecuteOracleNonQuery( } else if (type == typeof(DataSet)) { // 返回DataSet DataSet ds = new DataSet(); using (OracleDataAdapter da = new OracleDataAdapter(cmd)) { da.Fill(ds); } retval = ds; } else if (type == typeof(DataTable)) { // 返回DataTable DataTable dt = new DataTable(); using (OracleDataAdapter da = new OracleDataAdapter(cmd)) { da.Fill(dt); } retval = dt; } else if (type == typeof(OracleDataReader)) { // 返回OracleDataReader retval = cmd.ExecuteReader(); } else if (type == typeof(object)) { // 返回第一个值 retval = cmd.ExecuteScalar(); } else { LogMessageHelper.LogERROR(string.Format("The type({0}) is not supported", type)); throw new Exception(string.Format("The type({0}) is not supported", type)); } // 清除命令 cmd.Parameters.Clear(); } catch (OracleException ex) { //记录数据库执行错误日志 StringBuilder parsdb = new StringBuilder(); parsdb.Append("{"); foreach (OracleParameter par in cmd.Parameters) { parsdb.AppendFormat("{0}:{1}", par.ParameterName, par.Value); } parsdb.Append("}"); Dictionary <string, string> pars = new Dictionary <string, string>(); pars.Add("command", cmd.CommandText); pars.Add("type", cmd.CommandType.ToString()); pars.Add("pars", parsdb.ToString()); LogMessageHelper.LogERROR("数据库执行错误", pars, null, ex); //重新抛出异常 throw ex; } finally { // 是否要关闭连接 if (type != typeof(OracleDataReader)) { if (_mustColseConnection == true) { _conn.Close(); _conn = null; } } } // 返回值 return((T)retval); }