编程语言
首页 > 编程语言> > c# – Report Viewer X Dapper

c# – Report Viewer X Dapper

作者:互联网

我正在使用Dapper向ReportDataSource提供查询.
但是,我有一个空报告,即使有一个IEnumerable加载数据.
当你花费数据表工作.

如何使用Dapper for 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(); –

解决方法:

看起来像Dapper现在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);
}

now supported也使用DataTable作为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.");
    }
}

标签:c,dapper,reportviewer
来源: https://codeday.me/bug/20190529/1177491.html