针对DataTable运行SQL

时间:2013-03-28 18:19:07

标签: asp.net sql ado.net datatable

我在ASP.NET 3.5中实际生成了DataTable,现在我想使用group by并使用SQL DataTable进行一些计算。

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

例如:

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

1 个答案:

答案 0 :(得分: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

相关问题