/// <summary> /// 执行查询,返回查询结果的第一行第一列 /// </summary> /// <typeparam name="T">返回值类型</typeparam> /// <param name="sql">SQL查询语句</param> /// <param name="parameters">参数集合</param> /// <returns>查询结果的第一行第一列</returns> public override T ExecuteScalar <T>(string sql, KdtParameterCollection parameters) { try { T value = default(T); // 执行SQL命令 using (AseCommand cmd = new AseCommand(ReplaceSqlText(ReplaceSqlText(sql, parameters), parameters), _sybaseCn)) { InitCommand(cmd); // 初始化 // 赋值参数 var hasConvertParams = ConvertToSqlParameter(parameters); foreach (var item in hasConvertParams) { cmd.Parameters.Add(item.Value); } value = cmd.ExecuteScalar().Convert <T>(); cmd.Cancel(); cmd.Dispose(); } return(value); } catch (Exception ex) { KdtLoger.Instance.Error(ex); throw new DataException(string.Format("执行查询,返回查询结果的第一行第一列错误,原因为:{0}", ex.Message)); } }
/// <summary> /// 执行非查询SQL语句 /// </summary> /// <param name="sql">SQL语句</param> /// <param name="parameters">参数集合</param> /// <returns>受影响的记录数</returns> public override int ExecuteNoQuery(string sql, KdtParameterCollection parameters) { try { int effected = 0; // 执行SQL命令 using (AseCommand cmd = new AseCommand(ReplaceSqlText(sql, parameters), _sybaseCn)) { InitCommand(cmd); // 初始化 // 赋值参数 var hasConvertParams = ConvertToSqlParameter(parameters); foreach (var item in hasConvertParams) { cmd.Parameters.Add(item.Value); } effected = cmd.ExecuteNonQuery(); cmd.Cancel(); cmd.Dispose(); } return(effected); } catch (Exception ex) { KdtLoger.Instance.Error(ex); throw new DataException(string.Format("执行非查询SQL语句错误,原因为:{0}", ex.Message)); } }
/// <summary> /// 读取KEY VALUE值 /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public override KeyValueCollection ExecuteKVCollection(string sql, KdtParameterCollection parameters) { try { KeyValueCollection entity = new KeyValueCollection(); // 执行SQL命令 using (AseCommand cmd = new AseCommand(ReplaceSqlText(sql, parameters), _sybaseCn)) { InitCommand(cmd); // 初始化 // 赋值参数 var hasConvertParams = ConvertToSqlParameter(parameters); foreach (var item in hasConvertParams) { cmd.Parameters.Add(item.Value); } // 执行填充数据 using (AseDataReader reader = cmd.ExecuteReader()) { entity = GetEntity(reader); // 反射参数值 ReflectParamValue(parameters, hasConvertParams); reader.Close(); reader.Dispose(); } cmd.Cancel(); cmd.Dispose(); } return(entity); } catch (AseException me) { KdtLoger.Instance.Error(me); throw new DataException(me.Message); } catch (Exception ex) { KdtLoger.Instance.Error(ex); throw new DataException(string.Format("执行SQL查询,返回数据集合错误,原因为:{0}", ex.Message)); } }