예제 #1
0
        /// <summary>
        /// 自定义查询
        /// </summary>
        /// <param name="sql">select a,b from tt where a= ? and b = ?</param>
        /// <param name="pars">object[],可夹杂  Alx.ORM.Core.Parameter实例</param>
        /// <returns></returns>
        public DataSet Query(string sql, IEnumerable <object> pars)
        {
            var cmd      = Provider.CreateCommand();
            var parNames = new List <string>();
            var parreg   = new Regex(@"\?");

            sql = parreg.Replace(sql, delegate(Match match)
            {
                var s = ParamPerFix + "par" + match.Index;
                parNames.Add(s);
                return(s);
            });
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection  = Connection;
            DbParameter par = null;

            parNames.Action((name, i) =>
            {
                par               = Provider.CreateParameter();
                par.Direction     = ParameterDirection.Input;
                par.ParameterName = name;
                if (pars.ElementAt(i) is Parameter)
                {
                    par.DbType = (pars.ElementAt(i) as Parameter).DbType;
                    //par.Value = Special.FixValue(Enumerable_my.ToList(pars.ElementAt(i) as IEnumerable)[1]);
                    par.Value = (pars.ElementAt(i) as Parameter).Value;
                }
                else
                {
                    par.Value = Special.FixValue(pars.ElementAt(i));
                }
                cmd.Parameters.Add(par);
            });
            var ada = Provider.CreateDataAdapter();

            ada.SelectCommand = cmd;
            var ds = new DataSet();

            ada.Fill(ds);
            return(ds);
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="Model"></typeparam>
        /// <param name="where">name = ? and age = ?</param>
        /// <param name="pars">有默认DbType规则,也可指定Parameter </param>
        /// <param name="skip"></param>
        /// <param name="take"></param>
        /// <returns></returns>
        public IEnumerable <Model> Query <Model>(string where, IEnumerable <object> pars, int skip, int take) where Model : TableBase, new()
        {
            var tableType = typeof(Model);
            var tableAttr = AttrCache.Get(tableType);
            var parNames  = new List <string>();
            var cmd       = Provider.CreateCommand();

            cmd.CommandText = Special.SqlSelect(tableAttr, where, parNames, skip, take);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection  = Connection;

            DbParameter par = null;

            parNames.Action((name, i) =>
            {
                par               = Provider.CreateParameter();
                par.Direction     = ParameterDirection.Input;
                par.ParameterName = name;
                par.Value         = Special.FixValue(pars.ElementAt(i));
                cmd.Parameters.Add(par);
            });
            try
            {
                var ada = Provider.CreateDataAdapter();
                ada.SelectCommand = cmd;
                var ds = new DataSet();
                ada.Fill(ds);
                return(TableBind <Model>(tableAttr, ds.Tables[0]));
                //var reader = cmd.ExecuteReader();
                //return ReaderBind<Model>(tableAttr, reader);
            }
            catch (DbException ex)
            {
                throw ex;
            }
        }
예제 #3
0
 public static List <bool> Action(this List <bool> table, string tableValues, Action action)
 {
     return(table.Action(action, tableValues));
 }