c# datatable select statement with dates

后端 未结 5 1331
难免孤独
难免孤独 2021-01-12 06:16

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 sel

相关标签:
5条回答
  • 2021-01-12 06:22

    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.

    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.

    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);
    
    0 讨论(0)
  • 2021-01-12 06:30

    I posted an answer on this post.

    DataTable Select by exact DateTime

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

    0 讨论(0)
  • 2021-01-12 06:34

    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.

    0 讨论(0)
  • 2021-01-12 06:40

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

    0 讨论(0)
  • 2021-01-12 06:42

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

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

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

    Your Select Statement will be like this.

      DataRow[] rows = newTable.Select("date >= #" + from_date + "# AND date <= #" + to_date + "#");
    
    0 讨论(0)
提交回复
热议问题