【问题标题】:DataTable To Load A List加载列表的数据表
【发布时间】:2016-11-28 06:20:23
【问题描述】:

我创建了一个存储过程 (SP),并以以下方式将其集成,效果很好:

已编辑:

private DataTable GetSPResult()
{
    int m = Convert.ToInt32(DropDownList1.SelectedValue);
    int k = Convert.ToInt32(DropDownList2.SelectedValue);

    DataTable ResultsTable = new DataTable();

    var context = new DemoEntities();
    var con = context.Database.Connection;
    var connectionState = con.State;

    try
    {
        using (context)
        {
            con.Open();

            using (var cmd = con.CreateCommand())
            {
                cmd.CommandText = "MonthlyConsumption"; //Here is the SP
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Para1", SqlDbType.Int));
                cmd.Parameters["@Para1"].Value = m;
                cmd.Parameters.Add(new SqlParameter("@Para2", SqlDbType.Int));
                cmd.Parameters["@Para2"].Value = k;

                using (var reader = cmd.ExecuteReader())
                {
                    ResultsTable.Load(reader);
                }
            }
        }
    }

    catch (Exception ex)
    {
        throw ex;
    }

    finally
    {
        if (con != null)
        {
            con.Close();
        }
    }

    return ResultsTable;
}

最后完成了以下操作:点击按钮后,我可以在报告中看到数据

protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dt = GetSPResult();
    ReportViewer1.Visible = true;

    ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report1.rdlc");
    ReportViewer1.LocalReport.DataSources.Clear();
    ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));    
}

输出:

但是当我尝试使用 ORM 将列表转换为 DataTable 时,它​​没有抛出异常,但报告中没有数据,如下所示:

输出:

这是我到目前为止使用 ORM 完成的代码 - 也可以工作的实体框架:顺便说一下,我设置断点用于调试目的,它获取值但不会在报告中返回数据

public DataTable GetSPResult()
{
    int m = Convert.ToInt32(DropDownList1.SelectedValue);
    int k = Convert.ToInt32(DropDownList2.SelectedValue);

    DataTable ResultsTable = new DataTable();

    var context = new DemoEntities();

    using (context)
    {
       var query = context.MonthlyConsumption(m, k).ToList();

       foreach (var item in query)
       {
            ResultsTable.Columns.Add("Store");
            ResultsTable.Columns.Add("Product");
            ResultsTable.Columns.Add("Jan");
            ResultsTable.Columns.Add("Feb");
            ResultsTable.Columns.Add("Mar");
            ResultsTable.Columns.Add("Apr");
            ResultsTable.Columns.Add("May");
            ResultsTable.Columns.Add("Jun");
            ResultsTable.Columns.Add("Jul");
            ResultsTable.Columns.Add("Aug");
            ResultsTable.Columns.Add("Sep");
            ResultsTable.Columns.Add("Oct");
            ResultsTable.Columns.Add("Nov");
            ResultsTable.Columns.Add("Dec");

            ResultsTable.Rows.Add(item.StoreName);
            ResultsTable.Rows.Add(item.ItemName);
            ResultsTable.Rows.Add(item.M1.Value);
            ResultsTable.Rows.Add(item.M2.Value);
            ResultsTable.Rows.Add(item.M3.Value);
            ResultsTable.Rows.Add(item.M4.Value);
            ResultsTable.Rows.Add(item.M5.Value);
            ResultsTable.Rows.Add(item.M6.Value);
            ResultsTable.Rows.Add(item.M7.Value);
            ResultsTable.Rows.Add(item.M8.Value);
            ResultsTable.Rows.Add(item.M9.Value);
            ResultsTable.Rows.Add(item.M10.Value);
            ResultsTable.Rows.Add(item.M11.Value);
            ResultsTable.Rows.Add(item.M12.Value);
        }
     }

   return ResultsTable;
}

注意和更新:我可以将 List 转换为 IDataReader 来加载它还是有什么简单的方法来完成它?我看过一些教程,其中作者使用foreach 循环迭代列表,然后将其绑定到DataTable。但我只是想简单地将列表加载到 DataTable。

【问题讨论】:

  • 如果你真的需要使用数据阅读器,那就自己实现一个。但是,如果您只需要将数据绑定到报表,请尝试使用 BindingSource 组件(我假设他们在这里使用的是 win 表单)。 BindingSource 可以与列表绑定。然后你可以用 BindingSouce 绑定你的报告
  • 解决方案与我的@user3185569 几乎相同。谢谢。我肯定会在帖子中看到替代方案。 @user1681317 没关系。我没明白我的意思是自己创建DataReader。我已经在项目(Web 窗体)中使用 EF,并混合了旧版 Sql 和 Linq 以使用 DataTable。所以我在想如果完全使用 EF 是否会有任何冲突或任何替代方案。
  • @AT-2016 您可以将列表转换为数据表,stackOverflow 提供了相关代码。搜索即可。
  • 我尝试将列表转换为DataTable@user3185569。但它在报告中没有显示任何数据。还有什么我需要做的,或者我错过了什么。实际上不确定-我已经使用将列表转换为DataTable的代码编辑了帖子。

标签: c# linq stored-procedures datatable


【解决方案1】:

终于搞定了。只需使用以下方法将列表转换为 DataTable:

public DataTable ToDataTable<T>(List<T> items)
{
   DataTable ResultsTable = new DataTable(typeof(T).Name);

   //Gets all the properties
   PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

   foreach (PropertyInfo prop in Props)
   {
      //Sets column names as Property names
      ResultsTable.Columns.Add(prop.Name);
   }

   foreach (T item in items)
   {
      var values = new object[Props.Length];

      for (int i = 0; i < Props.Length; i++)
      {
         //Inserts property values to datatable rows
         values[i] = Props[i].GetValue(item, null);
      }

      ResultsTable.Rows.Add(values);
   }

   return ResultsTable;
}

【讨论】:

  • 只需将列表作为方法或函数的参数传递即可。
猜你喜欢
  • 2019-11-27
  • 2017-02-24
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 2015-06-06
  • 1970-01-01
  • 2012-05-04
相关资源
最近更新 更多