/// <summary> /// 数据阅读器 /// </summary> /// <param name="cmdType"></param> /// <param name="cmdText"></param> /// <param name="spr"></param> /// <returns></returns> public IDataReader ExecuteReader(CommandType cmdType, string cmdText, List <SqlParameter> spr) { List <MySqlParameter> listParam = SqlConvertHelper.GetMySqlParams(spr); MySqlConnection conn = GetConnection(false); conn.Open(); MySqlCommand cmd = new MySqlCommand(cmdText, conn); try { cmd.CommandType = cmdType; cmd.CommandTimeout = 300; if (spr != null) { foreach (MySqlParameter s in listParam) { cmd.Parameters.Add(s); } } MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); return(dr); } catch (Exception ex) { _logger.Error(ex.Message, ex); } finally { cmd.Parameters.Clear(); } return(null); }
/// <summary> /// 获取数据 /// </summary> /// <param name="cmdType"></param> /// <param name="cmdText"></param> /// <param name="spr"></param> /// <returns></returns> public DataSet GetDataSet(CommandType cmdType, string cmdText, List <SqlParameter> spr) { List <MySqlParameter> listParam = SqlConvertHelper.GetMySqlParams(spr); DataSet ds = new DataSet(); using (MySqlConnection conn = GetConnection(false)) { conn.Open(); MySqlDataAdapter oda = new MySqlDataAdapter(cmdText, conn); try { oda.SelectCommand.CommandType = cmdType; oda.SelectCommand.CommandTimeout = 300; if (spr != null) { foreach (MySqlParameter s in listParam) { oda.SelectCommand.Parameters.Add(s); } } oda.Fill(ds); } catch (Exception ex) { _logger.Error(ex.Message, ex); } finally { oda.SelectCommand.Parameters.Clear(); conn.Close(); } return(ds); } }
/// <summary> /// 返回影响的行数 /// </summary> /// <param name="commandType"></param> /// <param name="cmdText"></param> /// <param name="spr"></param> /// <returns></returns> public int ExecuteNonQuery(CommandType commandType, string cmdText, List <SqlParameter> spr) { int i = 0; List <MySqlParameter> listParam = SqlConvertHelper.GetMySqlParams(spr); using (MySqlConnection conn = GetConnection()) { conn.Open(); MySqlCommand cmd = new MySqlCommand(cmdText, conn); //事物 MySqlTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted); try { cmd.Parameters.Clear(); cmd.CommandType = commandType; cmd.CommandTimeout = 300; cmd.Transaction = trans; if (spr != null) { foreach (MySqlParameter s in listParam) { cmd.Parameters.Add(s); } ; } i = cmd.ExecuteNonQuery(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); _logger.Error(ex.Message, ex); } finally { conn.Close(); cmd.Parameters.Clear(); } return(i); } }
/// <summary> /// 返回第一行第一列的值 /// </summary> /// <param name="cmdType"></param> /// <param name="cmdText"></param> /// <param name="spr"></param> /// <returns></returns> public string ExecuteScalar(CommandType cmdType, string cmdText, List <SqlParameter> spr) { List <MySqlParameter> listParam = SqlConvertHelper.GetMySqlParams(spr); using (MySqlConnection conn = GetConnection(false)) { conn.Open(); MySqlCommand cmd = new MySqlCommand(cmdText, conn); try { cmd.CommandType = cmdType; cmd.CommandTimeout = 300; if (spr != null) { foreach (MySqlParameter s in listParam) { cmd.Parameters.Add(s); } } if (cmd.ExecuteScalar() != null) { return(cmd.ExecuteScalar().ToString()); } } catch (Exception ex) { _logger.Error(ex.Message, ex); } finally { cmd.Parameters.Clear(); conn.Close(); } return(null); } }