当前位置: 首页>>代码示例>>C#>>正文


C# IDataAccess.CreateCommand方法代码示例

本文整理汇总了C#中IDataAccess.CreateCommand方法的典型用法代码示例。如果您正苦于以下问题:C# IDataAccess.CreateCommand方法的具体用法?C# IDataAccess.CreateCommand怎么用?C# IDataAccess.CreateCommand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IDataAccess的用法示例。


在下文中一共展示了IDataAccess.CreateCommand方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateDataSetPage

        /// <summary>
        /// 按照分页查询数据
        /// </summary>
        /// <param name="dataAccess">数据访问器</param>
        /// <param name="whereString">查询条件</param>
        /// <param name="orderString">排序或分组条件</param>
        /// <param name="pageStart">查询页编号</param>
        /// <param name="pageSize">每页数据量</param>
        /// <returns></returns>
        public IDbCommand CreateDataSetPage(IDataAccess dataAccess, string whereString, string orderString, int pageStart, int pageSize)
        {
            string strOrderby = string.Empty;
            string strWhere = string.Empty;
            int index = -1;
            if (!string.IsNullOrEmpty(orderString))
            {
                index = orderString.ToUpper().IndexOf("ORDER BY");
                strOrderby = "ORDER BY " + (index == -1 ? orderString.Trim() : orderString.Substring(index + 9).Trim());
            }
            if (!string.IsNullOrEmpty(whereString))
            {
                index = whereString.ToUpper().IndexOf("WHERE");
                strWhere = "WHERE " + (index == -1 ? whereString.Trim() : whereString.Substring(index + 6).Trim());
            }

            IDbCommand dbCommand = dataAccess.CreateCommand();
            dbCommand.CommandType = CommandType.Text;
            string sqlText = null;

            long rowStart = pageStart < 1 ? 1 : (pageStart - 1) * pageSize + 1;
            long rowEnd = rowStart + pageSize;
            switch (dataAccess.DataBaseType)
            {
                case DataBaseType.Sql:
                    if (rowStart <= 1L)
                    {
                        //sqlText = string.Format("SELECT TOP {0} {1} FROM {2} WITH(NOLOCK){3} ORDER BY {4}",
                        sqlText = string.Format("SELECT TOP {0} {1} FROM {2} {3} {4}",
                         new object[] { rowEnd, "*", this.TableName, strWhere, strOrderby });
                    }
                    else
                    {
                        sqlText = string.Format("WITH temptable AS (SELECT ROW_NUMBER() OVER({0}) AS 'RowNo',{1} FROM {2} {3}) SELECT * FROM temptable WHERE RowNo BETWEEN {4} AND {5}",
                            new object[] { strOrderby, "*", this.TableName, strWhere, rowStart, rowEnd });
                    }
                    break;
                case DataBaseType.Oracle:
                case DataBaseType.Odac:
                    strWhere = "AND" + strWhere.Substring(5);
                    sqlText = string.Format("SELECT * FROM (SELECT a.*, ROWNUM rn FROM (SELECT * FROM {0} {1}) a WHERE ROWNUM <= {2} {3}) WHERE rn >= {4}",
                        new object[] { this.TableName, strOrderby, rowEnd, strWhere, rowStart });
                    break;
                case DataBaseType.OleDb:
                    throw new Exception("没有设定");
            }

            dbCommand.CommandText = sqlText;
            return dbCommand;
        }
开发者ID:joeiren,项目名称:ePort,代码行数:59,代码来源:EntityAdapter.cs


注:本文中的IDataAccess.CreateCommand方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。