报告查看器 X Dapper

问题描述 投票:0回答:5

我正在使用 Dapper 向

ReportDataSource
提供查询。 但是,即使加载了
IEnumerable
数据,我也有一个空报告。 当您花
Datatable
工作时。

如何使用 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(); –
c# reportviewer dapper
5个回答
10
投票

看起来 Dapper 现在支持数据表...

来自测试

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正在使用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.");
    }
}

2
投票

由于我找不到另一种方法将 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

1
投票

使用Dapper从SqliteDB获取DataTable。

  1. 在方法内部传递“select*from table”等查询。

  2. 通过执行查询形成列表。

  3. 然后将对象序列化成Json,然后将Json反序列化 进入数据表。

    public DataTable method(string query)
    {
       string connection = @"Data Source= C:\User\DBFolder\sampleDB.db;Version=3;New=False;Compress=True;";
    
       using (IDbConnection dbConnection = new SQLiteConnection(connection))
        {
            dbConnection.Open();
            var output1 = dbConnection.Query(query).ToList();
            dbConnection.Close();
            var json = JsonConvert.SerializeObject(output1);
            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
            return dt;
        }
    }
    

0
投票

不确定 Dapper 是如何工作的,但数据表是这样绑定的:

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
投票

基于这个答案,这是我最小的 Dapper 扩展,用于查询 SQL Server 的

DataTable
并可选择传递查询参数:

public static class DapperExtensions
{
    public static async Task<DataTable> QueryDataTable(
        string connectionString, 
        string sql, 
        object? param = null)
    {
        await using var con = new SqlConnection(connectionString);
        await con.OpenAsync();

        var table = new DataTable();
        table.Load(await con.ExecuteReaderAsync(sql, param));

        return table;
    }
}

使用示例:

var table = await QueryDataTable(
    "Server=myServer;Database=myDb;User Id=myUser;Password=myPw", 
    "SELECT * FROM Customers WHERE ID > @id", 
    new { id = 5 });

另一个例子:

var table = await QueryDataTable(
    "Server=myServer;Database=myDb;User Id=myUser;Password=myPw", 
    "SELECT * FROM Customers WHERE IsLocked = 0");

您可能想将

SqlConnection
更改为另一种数据库类型,例如
NpgsqlConnection
用于 PostgreSQL。

© www.soinside.com 2019 - 2024. All rights reserved.