Exemplo n.º 1
0
        /// <summary>
        /// Gets the count of all rows in the table that satisfy a given condition.
        /// </summary>
        /// <param name="table">Name of the table whose rows are to be counted</param>
        /// <param name="where">Conditional expression, such as could be placed on the end of a SQL WHERE clause</param>
        /// <returns>The count of all rows in the table satisfying the condition</returns>
        /// <exception cref="BadQuerySyntaxException">the SQL WHERE syntax is invalid</exception>
        /// <exception cref="InstallerException">the View could not be executed</exception>
        /// <exception cref="InvalidHandleException">the Database handle is invalid</exception>
        public int CountRows(string table, string where)
        {
            if (string.IsNullOrWhiteSpace(table))
            {
                throw new ArgumentNullException("table");
            }

            int count;

            using (View view = this.OpenView(
                       "SELECT `{0}` FROM `{1}`{2}",
                       this.Tables[table].PrimaryKeys[0],
                       table,
                       (where != null && where.Length != 0 ? " WHERE " + where : "")))
            {
                view.Execute();
                for (count = 0; ; count++)
                {
                    // Avoid creating unnecessary Record objects by not calling View.Fetch().
                    int  recordHandle;
                    uint ret = RemotableNativeMethods.MsiViewFetch((int)view.Handle, out recordHandle);
                    if (ret == (uint)NativeMethods.Error.NO_MORE_ITEMS)
                    {
                        break;
                    }

                    if (ret != 0)
                    {
                        throw InstallerException.ExceptionFromReturnCode(ret);
                    }

                    RemotableNativeMethods.MsiCloseHandle(recordHandle);
                }
            }
            return(count);
        }
Exemplo n.º 2
0
 protected override bool ReleaseHandle()
 {
     return(RemotableNativeMethods.MsiCloseHandle((int)this.handle) == 0);
 }