【问题标题】:Running SQL Against a DataTable针对数据表运行 SQL
【发布时间】:2013-03-19 07:59:30
【问题描述】:

我已经在 ASP.NET 3.5 中务实地生成了一个 DataTable,现在我想在这个 DataTable 上使用 group by 并使用 SQL 进行一些计算。

我的问题是:是否可以针对这个DataTable 编写一个新的 SQL 查询并生成一个新的更新的DataTable

例如:

select ID, sum(Rate) 
from **dataTable** 
group by ID

【问题讨论】:

  • 不,不是真的 - DataTable 不是支持 SQL 的数据库 - 它只是内存中的数据结构......
  • 您从哪里获取 DataTable 的数据?
  • @MikeSmithDev 我在 ASP.NET 中编写了一个函数来查询 SQL 服务器,然后我对该数据进行了一些计算并将该数据添加到 DataTable 中,该函数实际上返回了 DataTable。我调用该函数的位置不同,我需要对生成的 DataTable 进行更多计算。
  • 仅供参考 - 无法判断您是否知道这一点,但 DataTable 类与 ASP.NET 无关。它是 ADO.NET 的一部分。

标签: asp.net sql ado.net datatable


【解决方案1】:

您无法针对Datatable 重新编写SQL,但您可以使用DataTable.DefaultView.RowFilterDataTable.DefaultView.Sort 字符串属性来进一步过滤您当前的数据集:

来自 MSDN 的示例:

using System;
using System.Data;
using System.Windows.Forms;

public class Form1 : Form {
   protected TextBox Text1;
   protected DataSet DataSet1;

   public static void Main() {
      DemostrateDataView();
   }

   private static void DemostrateDataView() {
      // Create a DataTable with one column
      DataTable dt = new DataTable("MyTable");
      DataColumn column = new DataColumn("Col", typeof(int));
      dt.Columns.Add(column); 

      // Add 5 rows on Added state 
      for (int i = 0; i < 5; i++) {
         DataRow row = dt.NewRow();
         row["Col"] = i;
         dt.Rows.Add(row);
      }

      // Add 5 rows on Unchanged state 
      for (int i = 5; i < 10; i++) {
         DataRow row = dt.NewRow();
         row["Col"] = i;
         dt.Rows.Add(row);
         // Calling AcceptChanges will make the DataRowVersion change from Added to Unchanged in this case
         row.AcceptChanges();
      }

      // Create a DataView
      DataView dv = new DataView(dt);

      Console.WriteLine("Print unsorted, unfiltered DataView");
      PrintDataView(dv);

      // Changing the Sort order to descending
      dv.Sort = "Col DESC";

      Console.WriteLine("Print sorted DataView. Sort = 'Col DESC'");
      PrintDataView(dv);

      // Filter by an expression. Filter all rows where column 'Col' have values greater or equal than 3
      dv.RowFilter = "Col < 3";

      Console.WriteLine("Print sorted and Filtered DataView by RowFilter. RowFilter = 'Col > 3'");
      PrintDataView(dv);

      // Removing Sort and RpwFilter to ilustrate RowStateFilter. DataView should contain all 10 rows back in the original order
      dv.Sort = String.Empty;
      dv.RowFilter = String.Empty;

      // Show only Unchanged rows or last 5 rows
      dv.RowStateFilter = DataViewRowState.Unchanged;

      Console.WriteLine("Print Filtered DataView by RowState. RowStateFilter =  DataViewRowState.Unchanged");
      PrintDataView(dv);
   }

   private static void PrintDataView(DataView dv) {
      // Printing first DataRowView to demo that the row in the first index of the DataView changes depending on sort and filters
      Console.WriteLine("First DataRowView value is '{0}'", dv[0]["Col"]);

      // Printing all DataRowViews 
      foreach (DataRowView drv in dv) {
         Console.WriteLine("\t {0}", drv["Col"]);
      }
   }
}

更多信息http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx

数据表分组方式https://stackoverflow.com/a/8472044/752527

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 2020-07-07
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多