1

I have a datatable with 4 columns and one of them is smalldatetime type. When I retrieve datatable from SQL base, I would like to query further through datatable, but only by a specific time. Is this possible?

I am tryin with something like this:

DataRow[] rows = dataset.Tables[0].Select("resStart = 'some date '" + myTime+"'");

and specifically I am trying to navigate through dates and times in datagrid so finally it dhould retreive one row by query like this

DataRow[] rows = dataset.Tables[0].Select("resStart = '" + myDate +" " + myTime+"'");
miller
  • 1,636
  • 3
  • 26
  • 55
  • 1
    You really need to provide more details. I don't even get the question. The obvious answer is "yes" but I guess it's not what you're expecting. – m4573r Sep 05 '12 at 10:44
  • You need to show the code you are using to retrieve from database. Basically you want to add a `where` clause with a `between` on the column. – Richard Schneider Sep 05 '12 at 10:50
  • Select * from tbale name Where CONVERT(VARCHAR(10),Colum_Name,8) = 'Your Spcifictime here' – rohan panchal Sep 05 '12 at 11:00

2 Answers2

1

If you want to query your data within your sourceCode DataTable class supports method SELECT which allows you to filter your table according to a given condition. Check out MSDN for a detailed explanation.

string filterExpression = "YOUR_SMALLDATE_COLUMN > #1/1/00#";
DataRow[] rows = YOUR_DATATABLE_VAR.Select(filterExpression);

// eg. print out results - or rebind data etc.
for(int i = 0; i < rows.Length; i ++)
{
    Debug.WriteLine(rows[i]["ANY_COLUMN_NAME"]);
}

Of course, you may also change your select statement to the database and append a whereClause.

Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
1

Is this possible?

Yes of course. For example by using Linq-To-DataSet:

var thisYearsRows = table
    .AsEnumerable()
    .Where(r => r.Field<DateTime>("SmalldateTimeField").Year == DateTime.Now.Year);

You need to add using System.Linq; and a reference to System.Data.DataSetExtensions.dll.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939