Пример #1
0
        /// <summary>
        /// 根据查询sql语句及参数,返回对应查询结果的对象
        /// </summary>
        /// <typeparam name="T">返回对象的类型</typeparam>
        /// <typeparam name="T1">查询语句中的参数对象类型,无则传入Object</typeparam>
        /// <param name="selstr">数据库查询语句</param>
        /// <param name="connEnumStr">数据库连接串</param>
        /// <param name="obj">查询语句中的参数对象,无则传入null</param>
        /// <returns></returns>
        public T SelectObjFromDB <T, T1>(string SelStr, string ConnEnumStr, T1 obj) where T : new()
        {
            DataAccess        dac = DataAccessFactory.CreateSqlServerInstance(ConnEnumStr);
            DataAccessCommand cmd = dac.CreateCommand(SelStr);

            if (obj != null)
            {
                cmd.SetParameterCollection <T1>(obj);
            }
            return(DAOHelper.ConvertDataReader2Model <T>(dac.ExecuteDataReader(cmd)));
        }
Пример #2
0
        /// <summary>
        /// 执行sql语句,并返回受影响的行数
        /// </summary>
        /// <typeparam name="T">sql语句中参数对象类型,无则传入Object</typeparam>
        /// <param name="sqlstr">sql语句</param>
        /// <param name="connEnumStr">数据库连接串</param>
        /// <param name="obj">语句中参数对象,无则传入null</param>
        /// <returns></returns>
        public int ExecuteNonQueryFromDB <T>(string SelStr, string ConnEnumStr, T obj)
        {
            DataAccess        dac = DataAccessFactory.CreateSqlServerInstance(ConnEnumStr);
            DataAccessCommand cmd = dac.CreateCommand(SelStr);

            if (obj != null)
            {
                cmd.SetParameterCollection <T>(obj);
            }
            return(dac.ExecuteNonQuery(cmd));
        }
Пример #3
0
        /// <summary>
        /// 获取分页结果
        /// </summary>
        /// <typeparam name="T1">查询参数类型,通常为ViewModel</typeparam>
        /// <param name="t">查询参数实例</param>
        /// <returns>返回分页结果,包含已分页后数据集</returns>
        public PageResult <T> GetPagerResult <T1>(T1 t)
        {
            if (string.IsNullOrEmpty(LastQueryCountStr))
            {
                LastQueryCountStr = GetLastQueryCountStr(QuerySqlStr);//查询结果数量语句
            }
            if (string.IsNullOrEmpty(LastQueryStr))
            {
                LastQueryStr = GetLastQueryStr(QuerySqlStr);//添加 ROW_NUMBER() over()后的查询语句
            }

            DataAccess access;

            if (string.IsNullOrEmpty(Connection))
            {
                access = DataAccessFactory.CreateSqlServerInstance(ConnectionEnum.SqlServerConnection);
            }
            else
            {
                access = DataAccessFactory.CreateSqlServerInstance(Connection);
            }
            DataAccessCommand command = access.CreateCommand();

            if (t != null)
            {
                command.SetParameterCollection <T1>(t);//添加参数集
            }

            /// 获取总记录数
            int ItemCount = 0;//所有记录数,默认为0

            command.Sql = LastQueryCountStr;
            object count = access.ExecuteScalar(command);

            if (count is int)
            {
                ItemCount = (int)count;
                // Result.TotalItemCount = ItemCount;
            }

            //需返回的分页结果实例
            PageResult <T> Result = new PageResult <T>(_PageSize, ItemCount, _CurrentPageIndex, string.Empty);

            if (Result.CurrentPageIndex > 0)
            {
                _CurrentPageIndex = Result.CurrentPageIndex;
            }
            ///获取分页后数据集
            command.Sql       = GetPagedSql();//获取分页查询语句
            Result.ResultList = DAOHelper.ConvertDataReader2ModelList <T>(access.ExecuteDataReader(command));

            return(Result);
        }