2013-07-19 199 views
4

我正在使用Dapper查詢ReportDataSource。 但是,即使使用IEnumerable加載的數據,我也有空的報告。 當你花了一個Datatable的作品。報告查看器X Dapper

如何使用Dapper爲ReportViewer傳遞查詢中的數據?

this.reportViewer.LocalReport.DataSources.Clear(); DataTable dt = new DataTable();

dt = CN.Query(Sql,param);

Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName,dt); this.reportViewer.LocalReport.DataSources.Add(rprtDTSource); this.reportViewer.RefreshReport(); -

回答

1

,因爲我可以找不到另一種方法將我的ReportViewer提供給查詢Dapper.Query,然後下載源代碼並添加下面的代碼。

#region CODTEC SISTEMAS 
    /// <summary> 
    /// Return a typed list of objects, reader is closed after the call 
    /// </summary> 
    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 
+0

我知道這個帖子是舊的,但你下載了精巧的源代碼並添加了這個方法;那是對的嗎 ? – Ken

0

不知道如何短小精悍的作品,但一個數據表上綁定這樣的:

DataTable dt = new DataTable(); 
DataColumn dc = dt.Columns.Add(); 
dc.ColumnName = "DataColumn1"; 
dc = dt.Columns.Add(); 

dc.ColumnName = "DataColumn2"; 
dt.Rows.Add(new object[] { "Frank", 32 }); 
this.reportViewer1.LocalReport.DataSources.Clear(); 

this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1_DataTable1", dt)); 
this.reportViewer1.RefreshReport(); 

,因爲我正在使用C#,我綁定的數據源是這樣的:

this.bindingSource1.DataSource = getListMethod(); // this method/property returns a list of objects 
this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Point", (this.bindingSource1))); 
//Point is the datatable name in my Dataset.xsd file 
+0

隨着DataTable沒有問題。但是,Dapper返回的類型不會。一旦常規參數非常複雜,我無法放棄。 – user2421145

3

看起來小巧玲瓏現在supports DataTable中...

test

public void ExecuteReader() 
{ 
    var dt = new DataTable(); 
    dt.Load(connection.ExecuteReader("select 3 as [three], 4 as [four]")); 
    dt.Columns.Count.IsEqualTo(2); 
    dt.Columns[0].ColumnName.IsEqualTo("three"); 
    dt.Columns[1].ColumnName.IsEqualTo("four"); 
    dt.Rows.Count.IsEqualTo(1); 
    ((int)dt.Rows[0][0]).IsEqualTo(3); 
    ((int)dt.Rows[0][1]).IsEqualTo(4); 
} 

而且nowsupported使用數據表作爲TableValueParameter:

public void DataTableParameters() 
{ 
    try { connection.Execute("drop proC#DataTableParameters"); } catch { } 
    try { connection.Execute("drop table #DataTableParameters"); } catch { } 
    try { connection.Execute("drop type MyTVPType"); } catch { } 
    connection.Execute("create type MyTVPType as table (id int)"); 
    connection.Execute("create proC#DataTableParameters @ids MyTVPType readonly as select count(1) from @ids"); 

    var table = new DataTable { Columns = { { "id", typeof(int) } }, Rows = { { 1 }, { 2 }, { 3 } } }; 

    int count = connection.Query<int>("#DataTableParameters", new { ids = table.AsTableValuedParameter() }, commandType: CommandType.StoredProcedure).First(); 
    count.IsEqualTo(3); 

    count = connection.Query<int>("select count(1) from @ids", new { ids = table.AsTableValuedParameter("MyTVPType") }).First(); 
    count.IsEqualTo(3); 

    try 
    { 
     connection.Query<int>("select count(1) from @ids", new { ids = table.AsTableValuedParameter() }).First(); 
     throw new InvalidOperationException(); 
    } catch (Exception ex) 
    { 
     ex.Message.Equals("The table type parameter 'ids' must have a valid type name."); 
    } 
}