コード例 #1
0
        private SqlCommand GetCommand(CommandContext context)
        {
            var        cullingOrderBy = DbUtility.CullingOrderBy(context.Command.CommandText);
            SqlCommand sqlCount       = $"SELECT COUNT(1) FROM ({cullingOrderBy}) TEMP";

            return(sqlCount);
        }
コード例 #2
0
        private int GetRecordCountFromDatabase(CommandContext context)
        {
            var count          = 0;
            var cullingOrderBy = DbUtility.CullingOrderBy(context.Command.CommandText);
            var sqlCount       = $"SELECT COUNT(1) FROM ({cullingOrderBy}) TEMP";

            using (var connection = context.Database.CreateConnection(DistributedMode.Slave))
            {
                connection.OpenClose(() =>
                {
                    using (var command = context.Database.Provider.CreateCommand(connection, null, sqlCount, parameters: context.Parameters))
                    {
                        using (var reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                switch (reader.GetFieldType(0).GetDbType())
                                {
                                case DbType.Decimal:
                                    count = (int)reader.GetDecimal(0);
                                    break;

                                case DbType.Int32:
                                    count = reader.GetInt32(0);
                                    break;

                                case DbType.Int64:
                                    count = (int)reader.GetInt64(0);
                                    break;
                                }
                            }
                        }
                    }
                });
            }

            return(count);
        }