473,473 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to retrieve multi-row blocks from a datatable?

Is there a concise/efficient way to retrieve blocks of rows from a
datatable with VB2005?

I've got a datatable (let's call it AllData), constructed
programmatically, that contains a lot of sequential scientific data,
each row being a point in a time-course potentially every minute and
with data maybe extending over many months. There's a column
(Date_Time) set as a primary key which contains a datetime stamp (as a
datetime type) for each row.

What I'd like to do is to query the datatable for all rows relating to
say a single day by coding something like (I know this isn't legal
VB):

Dim DailyData as datatable = AllData.Select(col("Date_Time") where
datetime start-time AND datetime < end-time)

I could obviously do something by iterating 'manually' over all the
rows in AllData, but I was hoping to find a simpler and maybe more
efficient approach. AllData is just an in-memory datatable so AIUI I
can't use a standard SQL query on the datatable as I could if it were
a database. And I'm stuck with VB2005 so I don't believe that I could
use LINQ, which might make this easy in eg VB2008. So are there any
other options please?
Nov 9 '08 #1
11 4611
I presume that AllData is a System.Data.DataTable object, as indicated in
your example line for the daily data.. You can extract a block of rows from
the table using criteria such as you have listed with an expression like:

Dim Daily() as DataRow
Dim FilterString as String = String.Format("datetime #{0}# AND datetime <
#{1}#", DateTime.Parse(start-time), DateTime.Parse(end-time))
Daily = AllData.Select(FilterString)

"John Dann" <ne**@prodata.co.ukwrote in message
news:v1********************************@4ax.com...
Is there a concise/efficient way to retrieve blocks of rows from a
datatable with VB2005?

I've got a datatable (let's call it AllData), constructed
programmatically, that contains a lot of sequential scientific data,
each row being a point in a time-course potentially every minute and
with data maybe extending over many months. There's a column
(Date_Time) set as a primary key which contains a datetime stamp (as a
datetime type) for each row.

What I'd like to do is to query the datatable for all rows relating to
say a single day by coding something like (I know this isn't legal
VB):

Dim DailyData as datatable = AllData.Select(col("Date_Time") where
datetime start-time AND datetime < end-time)

I could obviously do something by iterating 'manually' over all the
rows in AllData, but I was hoping to find a simpler and maybe more
efficient approach. AllData is just an in-memory datatable so AIUI I
can't use a standard SQL query on the datatable as I could if it were
a database. And I'm stuck with VB2005 so I don't believe that I could
use LINQ, which might make this easy in eg VB2008. So are there any
other options please?
Nov 10 '08 #2
On Mon, 10 Nov 2008 11:53:00 +1100, "James Hahn" <jh***@yahoo.com>
wrote:
>I presume that AllData is a System.Data.DataTable object, as indicated in
your example line for the daily data.. You can extract a block of rows from
the table using criteria such as you have listed with an expression like:

Dim Daily() as DataRow
Dim FilterString as String = String.Format("datetime #{0}# AND datetime <
#{1}#", DateTime.Parse(start-time), DateTime.Parse(end-time))
Daily = AllData.Select(FilterString)
Many thanks. I'll be able to take a detailed look at this a little
later in the day. Yes in my example AllData was indeed a standard
DataTable object.

Ideally what I'd like to return is another - obviously smaller -
DataTable. Is there a neat (I'm thinking single line of code) way of
reassembling that array of datarows (ie Daily() ) back into a
DataTable? Or do I need to somehow clone the schema from the original
datatable and instantiate a new empty datatable and then iterate
through all the rows in Daily() and add each in turn to the new
datatable?
Nov 10 '08 #3
On Mon, 10 Nov 2008 11:53:00 +1100, "James Hahn" <jh***@yahoo.com>
wrote:
>I presume that AllData is a System.Data.DataTable object, as indicated in
your example line for the daily data.. You can extract a block of rows from
the table using criteria such as you have listed with an expression like:

Dim Daily() as DataRow
Dim FilterString as String = String.Format("datetime #{0}# AND datetime <
#{1}#", DateTime.Parse(start-time), DateTime.Parse(end-time))
Daily = AllData.Select(FilterString)
Sadly I can't get this to work. In the above FilterString definition
if I use:

Dim FilterString as String = String.Format("Date_Time #{0}# AND
DateTime ,<#{1}#", DateTime.Parse("1/5/2008"), DateTime.Parse(
"13/5/2008"))

(Date_Time is the column name for my datetime column and I'm working
with some data from May 2008) then I get:

FilterString = "Date_Time >#1/5/2008 00:00:00# AND Date_Time
<#13/5/2008 00:00:00#"

which seems as expected. But at the line

Daily = AllData.Select(FilterString)

a 'FormatException not handled' error 'String was not recognised as a
valid DateTime' is thrown.

It seems like this suggestion is trying to select from a datetime type
column by searching on a string type, which maybe (?) can't work?

Is there an error in my code snippett that I haven't spotted? Or is
there some other way of formatting the filter string that will filter
on datetime types?
Nov 11 '08 #4
"John Dann" <ne**@prodata.co.ukschrieb
Dim FilterString as String = String.Format("Date_Time #{0}# AND
DateTime ,<#{1}#", DateTime.Parse("1/5/2008"), DateTime.Parse(
"13/5/2008"))

(Date_Time is the column name for my datetime column and I'm working
with some data from May 2008) then I get:

FilterString = "Date_Time >#1/5/2008 00:00:00# AND Date_Time
<#13/5/2008 00:00:00#"
You'd better display the value during debugging because this string is not
the one created by the assignment. In the assignment, you have a "," too
much between "DateTime" and "<".

In addition, you can use date literals instead of parsing a string.

"13/5/2008" dosn't work because there is not 13th month.

You should also be aware of different date/time formats depending on the
current locale, so I recommend using a determined format:

Const DateTimeFormat As String = "\#MM\/dd\/yyyy HH\:mm\:ss\#"

Dim MinDate = #5/1/2008#
Dim MaxDate = #5/13/2008#

Dim FilterString As String = String.Format( _
"test {0} AND test <{1}", _
mindate.ToString(DateTimeFormat), _
maxdate.ToString(DateTimeFormat) _
)

Armin

Nov 11 '08 #5
On Tue, 11 Nov 2008 16:32:48 +0100, "Armin Zingler"
<az*******@freenet.dewrote:
>
You'd better display the value during debugging because this string is not
the one created by the assignment. In the assignment, you have a "," too
much between "DateTime" and "<".
Many thanks -that's very useful.

Sorry I don't have a news client on my VB development PC so I was
copying manually and obviously introduced an error. Here's the actual
string:

Date_Time >#01/05/2008 00:00:00# AND Date_Time <#13/05/2008 00:00:00#

which looks as intended to me.

However, the actual mindate and maxdate values (as you've named them)
are actually pre-existing datetime-type values in my code. If I print
one of these values in a msgbox it looks like:

13/05/2008 00:00:00 (UK locale)

So I've rewritten your example as follows:

Const DateTimeFormat As String = "dd\/MM\/yyyy HH\:mm\:ss"

Dim FilterString As String = String.Format("Date_Time >{0} AND
Date_Time <{1}", _
mindate.ToString(DateTimeFormat), maxdate.ToString(DateTimeFormat))
But the datatable.Select operation is giving a SyntaxErrorException
saying 'Missing operand after '00' operator'. So it looks like I'm
making progress in that the previous FormatException error has
vanished. But there's clearly something about format string that's not
quite right. Unfortunately I can't spot what that is. (NB Code above
is cut and paste across machines so is definitely exactly as is
written in the working code - other than line wrap.)
Nov 11 '08 #6
"John Dann" <ne**@prodata.co.ukschrieb
Const DateTimeFormat As String = "dd\/MM\/yyyy HH\:mm\:ss"
The format string I posted was:

Const DateTimeFormat As String = "\#MM\/dd\/yyyy HH\:mm\:ss\#"

Armin
Nov 11 '08 #7
Hi John,

I have done what you are trying to do. It is doable, but it is not a
one liner. Essentially, you need/want to execute some sql operations on
the fly based on the current rows you are working with. You have 2
options.

The 1st option is to go with the Rows retrieved from the
ds.Table.Select("StartDate >= '" & SomeStartDate.ToString & "' And
StartDate <= '" & someEndDate.ToString & "'")

Then loop through this datarow array into a dataTable you create in code
and populate the datatable. Kinda kludgy but doable.

The 2nd option is to push the data in your local datatable back to sql
server to a #tmp table and then query the #tmp table. This gives you a
little more flexibility. The caveat is that you have to create the #tmp
table in your code first (there is no escaping having to create a table
in code first - either a #tmp table or a local dataTable). Here is an
example:

---------------------------------------
'--da4 is a sqlDataAdapter

da4.SelectCommand.CommandText = "If (object_id('tempdb..#tmp1') is not
null) drop table #tmp1"
da4.SelectCommand.ExecuteNonQuery()

'--create #tmp1 in TempDB
da4.SelectCommand.CommandText = "Create Table #tmp1(FirstDate datetime,
ListNo varchar(2))"
da4.SelectCommand.ExecuteNonQuery()

'--now copy the #tmp1 structure to memory for app usage
da4.SelectCommand.CommandText = "Select * From #tmp1"
da4.Fill(dsFL, "tmpLocal")

da4.InsertCommand.CommandText = "Insert Into #tmp1(FirstDate, ListNo)
Select @FirstDate, @ListNo"

da4.InsertCommand.Parameters.Add("@FirstDate", SqlDbType.DateTime, 8,
"FirstDate")
da4.InsertCommand.Parameters.Add("@ListNo", SqlDbType.VarChar, 2,
"ListNo")

Dim reader As DataTableReader =
dataset1.YourLocalDataTable.CreateDataReader

'--set up the data push here
dataset1.Tables("tmpLocal").Load(reader, LoadOption.Upsert)

'--This is where you push the data
'--from yourLocalTable to #tmp1
da4.Update(dsFL, "tmpLocal")

'--now you can use tSql on #tmp1
da4.SelectCommand.CommandText = "Select * From #tmp1 Where Some
Condition"
da4.Fill(dataset1, "tblResult")

Datagridview1.DataSource = dataset1.Tables("tblResult")

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Nov 11 '08 #8
On Tue, 11 Nov 2008 18:08:08 +0100, "Armin Zingler"
<az*******@freenet.dewrote:
>"John Dann" <ne**@prodata.co.ukschrieb
>Const DateTimeFormat As String = "dd\/MM\/yyyy HH\:mm\:ss"

The format string I posted was:

Const DateTimeFormat As String = "\#MM\/dd\/yyyy HH\:mm\:ss\#"
Yes, I know. The thinking was that in your example mindate was
explicitly bounded by # characters. In my code, mindate is a
pre-assigned datetime type that, so far as I know, has no bounding #
characters, so I took them out. If I use the format string almost
exactly as you posted:

Const DateTimeFormat As String = "\#dd\/MM\/yyyy HH\:mm\:ss\#"

(pasted from my code) then I revert to the original 'string was not
recognised as a valid datetime' error. I've obviously interchanged dd
and MM compared to your example, but that's how datetime values seem
to appear to my system.

Maybe I should be adding bounding # characters to the formatted
values? ie instead of:

mindate.ToString(DateTimeFormat)

it should be

"#" & mindate.ToString(DateTimeFormat) & "#"

Sorry there's obviously something that I don't understand about the
exact usage and syntax of datetime values. But if you can spot any
further errors that I'm committing then I'd be most grateful.
Nov 11 '08 #9
On Tue, 11 Nov 2008 09:28:45 -0800, Rich P <rp*****@aol.comwrote:
>The 1st option is to go with the Rows retrieved from the
ds.Table.Select("StartDate >= '" & SomeStartDate.ToString & "' And
StartDate <= '" & someEndDate.ToString & "'")

Then loop through this datarow array into a dataTable you create in code
and populate the datatable. Kinda kludgy but doable.
This is what I'm currently trying to do, but seeing a problem with. I
was hoping I could do:

Dim Daily() As DataRow
Daily = MonthlyRawDT.Select(FilterString) ' FilterString set elsewhere

Dim DailyRawDT As DataTable = MonthlyRawDT.Clone()
For Each dr As DataRow In Daily
DailyRawDT.Rows.Add(dr)
Next

So:

MonthlyRawDT is my parent datatable from which I'm trying to extract a
subset of rows.

Daily() is the array of datarows populated by the .Select method.

Then I was thinking that I needed to instantiate a new datatable with
the same schema as the original parent datatable and iterate through
the datarows array adding one row at a time to the new datatable. But
I get the error that dr already belongs to another datatable.

Have I miscoded this (been a long day today!) or do I need to iterate
through each row item by item. That would be kludgy!

I guess VB2008 and LINQ really might make this exercise simpler?
Nov 11 '08 #10
do a console.writeline (or debug.print) on your date vars to see what
kind of data value your are passing in to

ds.table.Select("StartDate = '"...

And don't forget that .Select(...) is the Where clause in a tsql
statement and uses the same syntax - you have to delimit your dates with
single quotes .Select("StartDate = '" & someDateVar.ToString & "'")

So the DateVar needs to be a string to fit into the Where Clause of
..Select("StartDate = '" & DateVar.ToString & "'")

The DataRow Array is OK if you only have a few rows to filter. But if
you have lots of rows (more than 50 say)you will be better off pushing
your data back to the server to a #tmp table and then use regular tsql
on the #tmp table. It may seem kludgy at first, but way more flexible
than looping through an array of datarows.
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Nov 11 '08 #11
You can copy the datarows into a new table that has been created with the
same structure as the original. .ImportRow is probably the easiest way to do
this. See, for instance:
http://ziver.blogspot.com/2007/08/da...easily-be.html

"John Dann" <ne**@prodata.co.ukwrote in message
news:vl********************************@4ax.com...
On Tue, 11 Nov 2008 09:28:45 -0800, Rich P <rp*****@aol.comwrote:
snip <

This is what I'm currently trying to do, but seeing a problem with. I
was hoping I could do:

Dim Daily() As DataRow
Daily = MonthlyRawDT.Select(FilterString) ' FilterString set elsewhere

Dim DailyRawDT As DataTable = MonthlyRawDT.Clone()
For Each dr As DataRow In Daily
DailyRawDT.Rows.Add(dr)
Next

So:

MonthlyRawDT is my parent datatable from which I'm trying to extract a
subset of rows.

Daily() is the array of datarows populated by the .Select method.

Then I was thinking that I needed to instantiate a new datatable with
the same schema as the original parent datatable and iterate through
the datarows array adding one row at a time to the new datatable. But
I get the error that dr already belongs to another datatable.

Have I miscoded this (been a long day today!) or do I need to iterate
through each row item by item. That would be kludgy!

I guess VB2008 and LINQ really might make this exercise simpler?
Nov 11 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
2
by: Martin | last post by:
I have a page with a form on it in which I have a series of input fields like so (note the sub-scripted name): <input type='text' name='rating' value='1'> <input type='text' name='rating'...
0
by: Crystal | last post by:
Hi there, I need to retrieve multi files information. Instead of creating my own file brower, I think I can use Windows Explore to navigate these files. Can someone tell me how I can get the file...
0
by: Soul | last post by:
Hi, In ASP.Net page, Is there a way to retrieve those selected text from a normal TextField? For example, I have a multi-line TextField for user to enter some text. There is also a Button if...
0
by: Brian Henry | last post by:
Since no one else knew how to do this I sat here all morning experimenting with this and this is what I came up with... Its an example of how to get a list of items back from a virtual mode list...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
13
by: no.mail.pls | last post by:
Hiya, How do i retreive fields with similar values from 2 tables? I tried to use (1) "SELECT * FROM $table1 as o , $table2 as p WHERE o.name like '%p.name%'"; but it retrieves nothing at...
6
by: Watermark | last post by:
i got problem with this i m implementing web based mail system. i want to access mbox and retrieve mail from it and display it on web page.. can anybody help me.
0
by: =?Utf-8?B?U291c3NhbiBNaWthZWw=?= | last post by:
Hi all, I am developing an multi users application. One of my need is to update all users rights that are stored in their own session. To do this I need to retrieve all the current available...
3
by: mistersulu | last post by:
Hi all: I'm using a wx.ListView object with a multi-threaded wxPython app. The list is dynamically generated and accessed across two or more threads. In spite of the fact that I have checks to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.