Example #1
0
        /// <summary>
        /// Execute a command that returns multiple result sets, and access each in turn.
        /// </summary>
        /// <param name="cnn">The connection to query on.</param>
        /// <param name="sql">The SQL to execute for this query.</param>
        /// <param name="param">The parameters to use for this query.</param>
        /// <param name="transaction">The transaction to use for this query.</param>
        /// <param name="commandTimeout">Number of seconds before command execution timeout.</param>
        /// <param name="commandType">Is it a stored proc or a batch?</param>
        public static SqlMapper.GridReader XQueryMultiple(this IDbConnection cnn, string queryId, object param = null,
                                                          IDbTransaction transaction = null, int?commandTimeout = null, CommandType?commandType = null)
        {
            var sql = XolidQuery.GetQuery(queryId, param);

            return(cnn.QueryMultiple(sql, param, transaction, commandTimeout, commandType));
        }
Example #2
0
        /// <summary>
        /// Execute parameterized SQL and return an <see cref="IDataReader"/>.
        /// </summary>
        /// <param name="cnn">The connection to execute on.</param>
        /// <param name="queryId">The queryId in XML Mapper to execute for this query.</param>
        /// <param name="param">The parameters to use for this command.</param>
        /// <param name="transaction">The transaction to use for this command.</param>
        /// <param name="commandTimeout">Number of seconds before command execution timeout.</param>
        /// <param name="commandType">Is it a stored proc or a batch?</param>
        /// <returns>An <see cref="IDataReader"/> that can be used to iterate over the results of the SQL query.</returns>
        /// <remarks>
        /// This is typically used when the results of a query are not processed by Dapper, for example, used to fill a <see cref="DataTable"/>
        /// or <see cref="T:DataSet"/>.
        /// </remarks>
        /// <example>
        /// <code>
        /// <![CDATA[
        /// DataTable table = new DataTable("MyTable");
        /// using (var reader = ExecuteReader(cnn, queryId, param))
        /// {
        ///     table.Load(reader);
        /// }
        /// ]]>
        /// </code>
        /// </example>
        public static IDataReader XExecuteReader(this IDbConnection cnn, string queryId, object param = null,
                                                 IDbTransaction transaction = null, int?commandTimeout = null, CommandType?commandType = null)
        {
            var sql = XolidQuery.GetQuery(queryId, param);

            return(cnn.ExecuteReader(sql, param, transaction, commandTimeout, commandType));
        }
Example #3
0
        /// <summary>
        /// Executes a single-row query, returning the data typed as <paramref name="type"/>.
        /// </summary>
        /// <param name="cnn">The connection to query on.</param>
        /// <param name="type">The type to return.</param>
        /// <param name="queryId">The queryId in XML Mapper to execute for this query.</param>
        /// <param name="param">The parameters to pass, if any.</param>
        /// <param name="transaction">The transaction to use, if any.</param>
        /// <param name="commandTimeout">The command timeout (in seconds).</param>
        /// <param name="commandType">The type of command to execute.</param>
        /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception>
        /// <returns>
        /// A sequence of data of the supplied type; if a basic type (int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is
        /// created per row, and a direct column-name===member-name mapping is assumed (case insensitive).
        /// </returns>
        public static object XQuerySingleOrDefault(this IDbConnection cnn, Type type, string queryId, object param = null,
                                                   IDbTransaction transaction = null, int?commandTimeout = null, CommandType?commandType = null)
        {
            var sql = XolidQuery.GetQuery(queryId, param);

            return(cnn.QuerySingleOrDefault(type, sql, param, transaction, commandTimeout, commandType));
        }
Example #4
0
        /// <summary>
        /// Executes a single-row query, returning the data typed as <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">The type of result to return.</typeparam>
        /// <param name="cnn">The connection to query on.</param>
        /// <param name="queryId">The queryId in XML Mapper to execute for this query.</param>
        /// <param name="param">The parameters to pass, if any.</param>
        /// <param name="transaction">The transaction to use, if any.</param>
        /// <param name="commandTimeout">The command timeout (in seconds).</param>
        /// <param name="commandType">The type of command to execute.</param>
        /// <returns>
        /// A sequence of data of the supplied type; if a basic type (int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is
        /// created per row, and a direct column-name===member-name mapping is assumed (case insensitive).
        /// </returns>
        public static T XQueryFirst <T>(this IDbConnection cnn, string queryId, object param = null,
                                        IDbTransaction transaction = null, int?commandTimeout = null, CommandType?commandType = null)
        {
            var sql = XolidQuery.GetQuery(queryId, param);

            return(cnn.QueryFirst <T>(sql, param, transaction, commandTimeout, commandType));
        }
Example #5
0
        /// <summary>
        /// Perform a multi-mapping query with 3 input types.
        /// This returns a single type, combined from the raw types via <paramref name="map"/>.
        /// </summary>
        /// <typeparam name="TFirst">The first type in the recordset.</typeparam>
        /// <typeparam name="TSecond">The second type in the recordset.</typeparam>
        /// <typeparam name="TThird">The third type in the recordset.</typeparam>
        /// <typeparam name="TReturn">The combined type to return.</typeparam>
        /// <param name="cnn">The connection to query on.</param>
        /// <param name="sql">The SQL to execute for this query.</param>
        /// <param name="map">The function to map row types to the return type.</param>
        /// <param name="param">The parameters to use for this query.</param>
        /// <param name="transaction">The transaction to use for this query.</param>
        /// <param name="buffered">Whether to buffer the results in memory.</param>
        /// <param name="splitOn">The field we should split and read the second object from (default: "Id").</param>
        /// <param name="commandTimeout">Number of seconds before command execution timeout.</param>
        /// <param name="commandType">Is it a stored proc or a batch?</param>
        /// <returns>An enumerable of <typeparamref name="TReturn"/>.</returns>
        public static IEnumerable <TReturn> XQuery <TFirst, TSecond, TThird, TReturn>(this IDbConnection cnn, string queryId,
                                                                                      Func <TFirst, TSecond, TThird, TReturn> map, object param = null, IDbTransaction transaction = null,
                                                                                      bool buffered = true, string splitOn = "Id", int?commandTimeout = null, CommandType?commandType = null)
        {
            var sql = XolidQuery.GetQuery(queryId, param);

            return(cnn.Query <TFirst, TSecond, TThird, TReturn>(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType));
        }
Example #6
0
        /// <summary>
        /// Executes a single-row query, returning the data typed as <paramref name="type"/>.
        /// </summary>
        /// <param name="cnn">The connection to query on.</param>
        /// <param name="type">The type to return.</param>
        /// <param name="queryId">The queryId in XML Mapper to execute for this query.</param>
        /// <param name="param">The parameters to pass, if any.</param>
        /// <param name="transaction">The transaction to use, if any.</param>
        /// <param name="buffered">Whether to buffer results in memory.</param>
        /// <param name="commandTimeout">The command timeout (in seconds).</param>
        /// <param name="commandType">The type of command to execute.</param>
        /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception>
        /// <returns>
        /// A sequence of data of the supplied type; if a basic type (int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is
        /// created per row, and a direct column-name===member-name mapping is assumed (case insensitive).
        /// </returns>
        public static IEnumerable <object> XQuery(this IDbConnection cnn, Type type, string queryId, object param = null,
                                                  IDbTransaction transaction = null, bool buffered = true, int?commandTimeout = null,
                                                  CommandType?commandType    = null)
        {
            var sql = XolidQuery.GetQuery(queryId, param);

            return(cnn.Query(type, sql, param, transaction, buffered, commandTimeout, commandType));
        }
Example #7
0
        /// <summary>
        /// Return a sequence of dynamic objects with properties matching the columns.
        /// </summary>
        /// <param name="cnn">The connection to query on.</param>
        /// <param name="queryId">The queryId in XML Mapper to execute for this query.</param>
        /// <param name="param">The parameters to pass, if any.</param>
        /// <param name="transaction">The transaction to use, if any.</param>
        /// <param name="buffered">Whether to buffer the results in memory.</param>
        /// <param name="commandTimeout">The command timeout (in seconds).</param>
        /// <param name="commandType">The type of command to execute.</param>
        /// <remarks>Note: each row can be accessed via "dynamic", or by casting to an IDictionary&lt;string,object&gt;</remarks>
        public static IEnumerable <dynamic> XQuery(this IDbConnection cnn, string queryId, object param = null, SqlTransaction transaction = null, bool buffered = true)
        {
            var sql = XolidQuery.GetQuery(queryId, param);

            return(cnn.Query(sql, param, transaction, buffered));
        }