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


C# QueryBuilder.WhereIn方法代码示例

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


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

示例1: CutRecentChangesIfNecessary

        /// <summary>
        /// Cuts the recent changes if necessary.
        /// </summary>
        private void CutRecentChangesIfNecessary()
        {
            ICommandBuilder builder = GetCommandBuilder();
            DbConnection connection = builder.GetConnection(connString);
            DbTransaction transaction = BeginTransaction(connection);

            QueryBuilder queryBuilder = new QueryBuilder(builder);

            string query = queryBuilder.SelectCountFrom("RecentChange");

            DbCommand command = builder.GetCommand(transaction, query, new List<Parameter>());

            int rows = ExecuteScalar<int>(command, -1, false);

            int maxChanges = int.Parse(host.GetSettingValue(SettingName.MaxRecentChanges));

            if(rows > maxChanges) {
                // Remove 10% of old changes to avoid 1-by-1 deletion every time a change is made
                int entriesToDelete = maxChanges / 10;
                if(entriesToDelete > rows) entriesToDelete = rows;
                //entriesToDelete += entriesToDelete / 10;

                // This code is not optimized, but it surely works in most DBMS
                query = queryBuilder.SelectFrom("RecentChange", new string[] { "Id" });
                query = queryBuilder.OrderBy(query, new string[] { "Id" }, new Ordering[] { Ordering.Asc });

                command = builder.GetCommand(transaction, query, new List<Parameter>());

                DbDataReader reader = ExecuteReader(command);

                List<int> ids = new List<int>(entriesToDelete);

                if(reader != null) {
                    while(reader.Read() && ids.Count < entriesToDelete) {
                        ids.Add((int)reader["Id"]);
                    }

                    CloseReader(reader);
                }

                if(ids.Count > 0) {
                    // Given that the IDs to delete can be many, the query is split in many chunks, each one deleting 50 items
                    // This works-around the problem of too many parameters in a RPC call of SQL Server
                    // See also CutLog

                    for(int chunk = 0; chunk <= ids.Count / MaxParametersInQuery; chunk++) {
                        query = queryBuilder.DeleteFrom("RecentChange");
                        List<string> parms = new List<string>(MaxParametersInQuery);
                        List<Parameter> parameters = new List<Parameter>(MaxParametersInQuery);

                        for(int i = chunk * MaxParametersInQuery; i < Math.Min(ids.Count, (chunk + 1) * MaxParametersInQuery); i++) {
                            parms.Add("P" + i.ToString());
                            parameters.Add(new Parameter(ParameterType.Int32, parms[parms.Count - 1], ids[i]));
                        }

                        query = queryBuilder.WhereIn(query, "Id", parms.ToArray());

                        command = builder.GetCommand(transaction, query, parameters);

                        if(ExecuteNonQuery(command, false) < 0) {
                            RollbackTransaction(transaction);
                            return;
                        }
                    }
                }
            }

            CommitTransaction(transaction);
        }
开发者ID:mono,项目名称:ScrewTurnWiki,代码行数:72,代码来源:SqlSettingsStorageProviderBase.cs


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