时间:2019-05-09 标签:c#datatableselect语句带日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10934615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:49:13  来源:igfitidea点击:

c# datatable select statement with dates

c#datetimeselectdatatable

提问by George Georgiou

i am trying to make a select statement on a datatable to get the row that is within the date range i am looking for. I am new to this an i dont quite understand how this select statement works. I tried to write this but is not working. Can you please give me a hand here. I am stuck

我试图在数据表上做一个选择语句来获取我正在寻找的日期范围内的行。我是新手,我不太明白这个 select 语句是如何工作的。我试图写这个,但不起作用。你能帮我一下吗?我被卡住了

foundRows = dt.Select("DATE1 <= '" + date1+ "' AND DATE2 >= '" + date1+ '"');

采纳答案by SASS_Shooter

Besides wrapping your dates with #, if date1 is a DateTime and not a string, you need to use the ToString(your date format) to get the correct sql statement. For debugging it make it easier if first you create a string containing your filter, then do the select using that string. Then you can look at the string and use that in the query builder to validate your sql.

除了用# 包裹你的日期,如果date1 是一个DateTime 而不是一个字符串,你需要使用ToString(你的日期格式)来获得正确的sql 语句。如果首先创建一个包含过滤器的字符串,然后使用该字符串进行选择,那么调试会更容易。然后您可以查看字符串并在查询构建器中使用它来验证您的 sql。

回答by DareDevil

This the Best Optimal Search Criteria I have Tested. you having to dates.

这是我测试过的最佳搜索标准。你必须约会。

From_Date = 12/01/2012 To_Date = 12/31/2012

From_Date = 12/01/2012 To_Date = 12/31/2012

and Your column in DataTable upon which you applying . (in my code 'date')

以及您在 DataTable 中应用的列。(在我的代码“日期”中)

Your Select Statement will be like this.

您的 Select 语句将是这样的。

  DataRow[] rows = newTable.Select("date >= #" + from_date + "# AND date <= #" + to_date + "#");

回答by Tomv

Using this inside an SSIS script component. I just used the example from above that included "#" around the dates. Also I converted each to string. This worked perfectly.

在 SSIS 脚本组件中使用它。我只是使用了上面的示例,其中在日期周围包含“#”。我也将每个转换为字符串。这工作得很好。

Just in case you want to know how I setup this up inside SSIS: First had a data flow using the recordset destination with an Object variable to store the recordset.

以防万一您想知道我如何在 SSIS 中进行设置:首先有一个数据流,使用记录集目标和对象变量来存储记录集。

in my script I included the variable as a read only.

在我的脚本中,我将该变量包含为只读。

In the main class...

在主课...

public class ScriptMain : UserComponent
{

OleDbDataAdapter a = new OleDbDataAdapter();
System.Data.DataTable AwardedVacTable = new System.Data.DataTable();
...
...

then in Pre-Execute...

然后在预执行...

public override void PreExecute()
{
    base.PreExecute();

    a.Fill(AwardedVacTable, Variables.rsAwardedVac);
...
...

then in a custom method accessed the datatable ...

然后在自定义方法中访问数据表...

String dtFilter = "EmployeeID = " + empId.ToString() + " AND (#" + Convert.ToString(StartDate) "# <= EndDate AND #" + Convert.ToString(StartDate) + "# >= StartDate" + " OR #" + Convert.ToString(StartDate.AddDays((double)numDays)) + "# >= StartDate AND #" + Convert.ToString(StartDate.AddDays((double)numDays)) + "# <= EndDate)";

DataRow[] Overlaps = AwardedVacTable.Select(dtFilter);

回答by Jianghua Guo

expression = "Date > #2015-1-1#"; DataRow[] foundRows = table.Select(expression); 与 select * from tablename where Date>'2015-1-1'

表达式 = "日期 > #2015-1-1#"; DataRow[] foundRows = table.Select(expression); 与 select * from tablename where Date>'2015-1-1'

回答by nikoo28

I posted an answer on this post.

我在这个帖子上发布了一个答案。

DataTable Select by exact DateTime

DataTable 按确切日期时间选择

You can use a similar approach by using ticks to select in a range.

您可以使用类似的方法,通过使用刻度在一个范围内进行选择。