Report Viewer X Dapper

前端 未结 4 1799
野的像风
野的像风 2021-01-18 16:11

I\'m feeding a ReportDataSource with a query using Dapper. However, I have an empty report, even with an IEnumerable loaded data. When you spend a

4条回答
  •  不要未来只要你来
    2021-01-18 16:13

    As I could not find another way to feed my ReportViewer to a query Dapper.Query then downloaded the source and added the code below.

        #region CODTEC SISTEMAS
        /// 
        /// Return a typed list of objects, reader is closed after the call
        /// 
        public static DataTable Query(this IDbConnection cnn, string sql, object param, IDbTransaction transaction, int? commandTimeout, CommandType? commandType)
        {
            var identity = new Identity(sql, commandType, cnn, typeof(DapperRow), param == null ? null : param.GetType(), null);
            var info = GetCacheInfo(identity);
    
            IDbCommand cmd = null;
            IDataReader reader = null;
    
            bool wasClosed = cnn.State == ConnectionState.Closed;
            try
            {
                cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, param, commandTimeout, commandType);
    
                if (wasClosed) cnn.Open();
                reader = cmd.ExecuteReader(wasClosed ? CommandBehavior.CloseConnection : CommandBehavior.Default);
                wasClosed = false; // *if* the connection was closed and we got this far, then we now have a reader
                // with the CloseConnection flag, so the reader will deal with the connection; we
                // still need something in the "finally" to ensure that broken SQL still results
                // in the connection closing itself
    
                DataTable dt = new DataTable();
                dt.Load(reader);
    
    
                // happy path; close the reader cleanly - no
                // need for "Cancel" etc
                reader.Dispose();
                reader = null;
    
                return dt;
            }
            finally
            {
                if (reader != null)
                {
                    if (!reader.IsClosed) try { cmd.Cancel(); }
                        catch { /* don't spoil the existing exception */ }
                    reader.Dispose();
                }
                if (wasClosed) cnn.Close();
                if (cmd != null) cmd.Dispose();
            }
        }
        #endregion
    

提交回复
热议问题