Exemple #1
0
        public PageResult <T> nativeQuerySql(string sql, string sqlCount, IDictionary <string, object> parms, int startIndex, int pageSize)
        {
            Debug.Assert(startIndex >= 0);
            Debug.Assert(pageSize > 0 && pageSize <= 100);
            List <T> list  = list = new List <T>();
            int      count = 0;

            iDB2Parameter[] db2Parms = null;
            if (parms != null)
            {
                List <iDB2Parameter> paramlist = new List <iDB2Parameter>();
                foreach (var item in parms)
                {
                    Debug.Assert(item.Value != null);
                    paramlist.Add(new iDB2Parameter('@' + item.Key, item.Value));
                }
                db2Parms = paramlist.ToArray();
            }
            try
            {
                count = Convert.ToInt32(DB2Helper.ExecuteScalar(DB2Helper.ConnectionString, CommandType.Text, sqlCount, db2Parms));
            }
            catch (Exception ex)
            {
                log.Error("SQL错误:" + ex.Message);
                log.Error("SQL错误:" + sqlCount);
                if (parms != null)
                {
                    log.Error("SQL错误:" + parms.toJson());
                }
                throw ex;
            }

            if (count > 0)
            {
                try {
                    using (iDB2DataReader rdr = DB2Helper.ExecuteReader(DB2Helper.ConnectionString, CommandType.Text, sql, db2Parms))
                    {
                        if (rdr.Read())
                        {
                            do
                            {
                                T t = Activator.CreateInstance <T>();
                                PropertyInfo[] propertyInfo = t.GetType().GetProperties();
                                int            i            = 0;
                                foreach (PropertyInfo pi in propertyInfo)
                                {
                                    try {
                                        pi.SetValue(t, rdr.GetValue(i++));
                                    }catch (Exception ex)
                                    {
                                        log.Error("字段类型错误," + pi.Name + ":" + ex.Message);
                                    }
                                }
                                list.Add(t);
                            } while (rdr.Read());
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error("SQL错误:" + ex.Message);
                    log.Error("SQL错误:" + sql);
                    if (parms != null)
                    {
                        log.Error("SQL错误:" + parms.toJson());
                    }
                    throw ex;
                }
            }
            return(new PageResult <T>(startIndex, pageSize, count, list));
        }