从DataTable获取记录 [英] Get Records from DataTable
本文介绍了从DataTable获取记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有DataTable有DataTime类型列,我想从Datetime列字段的小时基础上从表中选择记录。
想要相同的Sql Query结果从TableName中选择ColumnName,其中datepart(HOUR,ColumName)= 09
09是小时值。
如果有任何想法请分享。
I have DataTable having DataTime type column, i want to select records from table on hour basic from Datetime column field.
want same result of Sql Query " select ColumnName from TableName where datepart(HOUR,ColumName)=09"
09 is hour value.
If have any idea please share.
推荐答案
你需要引用System.Data.DataSetExtensions程序集并使用System.Data命名空间。
You need to reference the System.Data.DataSetExtensions assembly and use the System.Data namespace.
dt =dt.AsEnumerable()
.Where(r=>r.Field<datetime>("DateCol").Month ==9)
.CopyToDataTable();
并阅读.NET Framework 3.5的文档
从查询创建DataTable(LINQ到DataSet) [ ^ ]
And read the documentation for .NET Framework 3.5
Creating a DataTable From a Query (LINQ to DataSet)[^]
below is sample code from above reference
// Bind the System.Windows.Forms.DataGridView object
// to the System.Windows.Forms.BindingSource object.
dataGridView.DataSource = bindingSource;
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable orders = ds.Tables["SalesOrderHeader"];
// Query the SalesOrderHeader table for orders placed
// after August 8, 2001.
IEnumerable<DataRow> query =
from order in orders.AsEnumerable()
where order.Field<DateTime>("OrderDate") > new DateTime(2001, 8, 1)
select order;
// Create a table from the query.
DataTable boundTable = query.CopyToDataTable<DataRow>();
// Bind the table to a System.Windows.Forms.BindingSource object,
// which acts as a proxy for a System.Windows.Forms.DataGridView object.
bindingSource.DataSource = boundTable;
当我使用9作为值时,该查询对我来说非常有效。
09似乎表示您将数据存储为文本而非日期时间,在这种情况下,您将格式化为日期时间。
That query works perfectly well for me when I use 9 as the value.
09 seems to indicate you are storing the data as text and not datetime in which case you are screwed until you change the format to datetime.
这篇关于从DataTable获取记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文