How to put database results into an array?

H

Haydnw

Hi,

I'd like to put a load of database results (several rows for 5 fields) into
a two-dimensional array. Now, this may be a really stupid question, but can
someone give me a pointer for how to do it? I can bind data to datagrids and
lists and stuff all day long, but can't seem to grasp this one. Any pointers
to a useful article / demo (or just any useful pointers!) would be much
appreciated.

Thanks,
Haydn
 
K

Karl

Haydn,
I'd love to know why you want to do this there is likely a better
alternative for whatever it is you are trying to do.

The only way I know how to do what you want to do is to manually loop
through your datasource:

DataTable dt = new DataTable();
dt.Columns.Add("column1");
dt.Columns.Add("column2");
dt.Columns.Add("column3");
for (int i = 0; i < 5; i++){
DataRow dr = dt.NewRow();
dr[0] = 1 * (i + 1);
dr[1] = 3 * (i + 1);
dr[2] = 2 * (i + 1);
dt.Rows.Add(dr);
}
int[,] array = new int[dt.Columns.Count,dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++){
for (int j = 0; j < dt.Columns.Count; j++){
array[j,i] = Convert.ToInt32(dt.Rows[j]);
}
}

Karl
 
H

Haydnw

Hi Karl, thanks for the reply. I'm fairly new to .NET in general, so it's
likely there is a better way! Basically I have a database containing info
about lots of different images, including the filename. The images are
viewed one at a time, on a page with 'Next' and 'Previous' buttons.
Obviously, the 'Next' button takes you to the next image in the database.
However, file names are numeric but not sequential so the current file name
has no relation to whatever comes next! So if I'm viewing an image with
filename 00003, I have no idea what the next one is.

Basically what I need is a way to return a set of results from access, and
say 'Get the SECOND row this time' or 'Get the THIRD row this time'. I don't
know any way of doing this. I thought that by dumping them in an array, I
would be able to reference the values in the array in a more structured
fashion because I know they are sequential starting from 0.

One alternative is to modify my SQL to get the top record where the filename
is greater than the one currently being viewed (if 'Next' button clicked) or
less than the one currently being viewed (if 'Previous' button clicked). I
realised this after my original post and it's now looking favourite! But I
thought another advantage of the array method might be that once I've
queried the database once to get all the info, I can maintain it through
page postbacks rather than querying the database for one record each time
the page is loaded. What are your views on this? Would the overheads of
maintaining a large two-dimensional array be greater than querying the
database each time the page loads?

Thank you very much,
Haydn



Karl said:
Haydn,
I'd love to know why you want to do this there is likely a better
alternative for whatever it is you are trying to do.

The only way I know how to do what you want to do is to manually loop
through your datasource:

DataTable dt = new DataTable();
dt.Columns.Add("column1");
dt.Columns.Add("column2");
dt.Columns.Add("column3");
for (int i = 0; i < 5; i++){
DataRow dr = dt.NewRow();
dr[0] = 1 * (i + 1);
dr[1] = 3 * (i + 1);
dr[2] = 2 * (i + 1);
dt.Rows.Add(dr);
}
int[,] array = new int[dt.Columns.Count,dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++){
for (int j = 0; j < dt.Columns.Count; j++){
array[j,i] = Convert.ToInt32(dt.Rows[j]);
}
}

Karl

Haydnw said:
Hi,

I'd like to put a load of database results (several rows for 5 fields) into
a two-dimensional array. Now, this may be a really stupid question, but can
someone give me a pointer for how to do it? I can bind data to datagrids and
lists and stuff all day long, but can't seem to grasp this one. Any pointers
to a useful article / demo (or just any useful pointers!) would be much
appreciated.

Thanks,
Haydn
 
K

Karl

Without knowing more, I would retrieve all records into a DataTable and
cache the datatable in the HttpRuntime.Cache object. I would use the
querystring to pass the index of the datatable to get. Something like (not
actually code as I'm writting it without my trusty editor, but enough to get
you started)


Page_Load
Dim imageIndex as integer
if Request.QueryString("index") is nothing then
imageIndex = 0
else
imageIndex = Request.Querystring("index")
end if

Dim table as DataTable = GetImages()
if imageIndex >= table.rows.Count then
imageIndex = 0
end if

DataRow dr = table.rows[imageIndex]
'do something with the datarow
End

Function GetImages() as DataTable
Dim cacheKey as string = "AllImages"
DataTable dt = ctype(cache(cacheKey), datatable)
if dt is nothing then
Dim Conn as new SqlConnect("...")
dim cmd as new SqlCommand("command here", conn)
dim da as new DataAdapter(cmd)
dt = new DataTable
da.fill(dt)
'close and dispose your stuff
Cache.Insert(cacheKey, dt, nothing, DateTime.Now.AddHours(1),
TimeSpan.Zero)
end if
return dt
End

You can then pass Index=(imageIndex+1) to get the next image and
Index=(imageIndex-1) to get the previous one. Hope that helps.

Karl


Haydnw said:
Hi Karl, thanks for the reply. I'm fairly new to .NET in general, so it's
likely there is a better way! Basically I have a database containing info
about lots of different images, including the filename. The images are
viewed one at a time, on a page with 'Next' and 'Previous' buttons.
Obviously, the 'Next' button takes you to the next image in the database.
However, file names are numeric but not sequential so the current file name
has no relation to whatever comes next! So if I'm viewing an image with
filename 00003, I have no idea what the next one is.

Basically what I need is a way to return a set of results from access, and
say 'Get the SECOND row this time' or 'Get the THIRD row this time'. I don't
know any way of doing this. I thought that by dumping them in an array, I
would be able to reference the values in the array in a more structured
fashion because I know they are sequential starting from 0.

One alternative is to modify my SQL to get the top record where the filename
is greater than the one currently being viewed (if 'Next' button clicked) or
less than the one currently being viewed (if 'Previous' button clicked). I
realised this after my original post and it's now looking favourite! But I
thought another advantage of the array method might be that once I've
queried the database once to get all the info, I can maintain it through
page postbacks rather than querying the database for one record each time
the page is loaded. What are your views on this? Would the overheads of
maintaining a large two-dimensional array be greater than querying the
database each time the page loads?

Thank you very much,
Haydn



"Karl" <none> wrote in message news:[email protected]...
Haydn,
I'd love to know why you want to do this there is likely a better
alternative for whatever it is you are trying to do.

The only way I know how to do what you want to do is to manually loop
through your datasource:

DataTable dt = new DataTable();
dt.Columns.Add("column1");
dt.Columns.Add("column2");
dt.Columns.Add("column3");
for (int i = 0; i < 5; i++){
DataRow dr = dt.NewRow();
dr[0] = 1 * (i + 1);
dr[1] = 3 * (i + 1);
dr[2] = 2 * (i + 1);
dt.Rows.Add(dr);
}
int[,] array = new int[dt.Columns.Count,dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++){
for (int j = 0; j < dt.Columns.Count; j++){
array[j,i] = Convert.ToInt32(dt.Rows[j]);
}
}

Karl

Haydnw said:
Hi,

I'd like to put a load of database results (several rows for 5 fields) into
a two-dimensional array. Now, this may be a really stupid question,
but
can
someone give me a pointer for how to do it? I can bind data to
datagrids
and
lists and stuff all day long, but can't seem to grasp this one. Any pointers
to a useful article / demo (or just any useful pointers!) would be much
appreciated.

Thanks,
Haydn

 
H

Haydnw

Karl,

Thank you so much! That works very well - excellently in fact! This
technique might prove for a couple of other pages in the site aswell. Thanks
again!

Haydn



Karl said:
Without knowing more, I would retrieve all records into a DataTable and
cache the datatable in the HttpRuntime.Cache object. I would use the
querystring to pass the index of the datatable to get. Something like (not
actually code as I'm writting it without my trusty editor, but enough to get
you started)


Page_Load
Dim imageIndex as integer
if Request.QueryString("index") is nothing then
imageIndex = 0
else
imageIndex = Request.Querystring("index")
end if

Dim table as DataTable = GetImages()
if imageIndex >= table.rows.Count then
imageIndex = 0
end if

DataRow dr = table.rows[imageIndex]
'do something with the datarow
End

Function GetImages() as DataTable
Dim cacheKey as string = "AllImages"
DataTable dt = ctype(cache(cacheKey), datatable)
if dt is nothing then
Dim Conn as new SqlConnect("...")
dim cmd as new SqlCommand("command here", conn)
dim da as new DataAdapter(cmd)
dt = new DataTable
da.fill(dt)
'close and dispose your stuff
Cache.Insert(cacheKey, dt, nothing, DateTime.Now.AddHours(1),
TimeSpan.Zero)
end if
return dt
End

You can then pass Index=(imageIndex+1) to get the next image and
Index=(imageIndex-1) to get the previous one. Hope that helps.

Karl


Haydnw said:
Hi Karl, thanks for the reply. I'm fairly new to .NET in general, so it's
likely there is a better way! Basically I have a database containing info
about lots of different images, including the filename. The images are
viewed one at a time, on a page with 'Next' and 'Previous' buttons.
Obviously, the 'Next' button takes you to the next image in the database.
However, file names are numeric but not sequential so the current file name
has no relation to whatever comes next! So if I'm viewing an image with
filename 00003, I have no idea what the next one is.

Basically what I need is a way to return a set of results from access, and
say 'Get the SECOND row this time' or 'Get the THIRD row this time'. I don't
know any way of doing this. I thought that by dumping them in an array, I
would be able to reference the values in the array in a more structured
fashion because I know they are sequential starting from 0.

One alternative is to modify my SQL to get the top record where the filename
is greater than the one currently being viewed (if 'Next' button
clicked)
or
less than the one currently being viewed (if 'Previous' button clicked). I
realised this after my original post and it's now looking favourite! But I
thought another advantage of the array method might be that once I've
queried the database once to get all the info, I can maintain it through
page postbacks rather than querying the database for one record each time
the page is loaded. What are your views on this? Would the overheads of
maintaining a large two-dimensional array be greater than querying the
database each time the page loads?

Thank you very much,
Haydn



"Karl" <none> wrote in message news:[email protected]...
Haydn,
I'd love to know why you want to do this there is likely a better
alternative for whatever it is you are trying to do.

The only way I know how to do what you want to do is to manually loop
through your datasource:

DataTable dt = new DataTable();
dt.Columns.Add("column1");
dt.Columns.Add("column2");
dt.Columns.Add("column3");
for (int i = 0; i < 5; i++){
DataRow dr = dt.NewRow();
dr[0] = 1 * (i + 1);
dr[1] = 3 * (i + 1);
dr[2] = 2 * (i + 1);
dt.Rows.Add(dr);
}
int[,] array = new int[dt.Columns.Count,dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++){
for (int j = 0; j < dt.Columns.Count; j++){
array[j,i] = Convert.ToInt32(dt.Rows[j]);
}
}

Karl

Hi,

I'd like to put a load of database results (several rows for 5 fields)
into
a two-dimensional array. Now, this may be a really stupid question, but
can
someone give me a pointer for how to do it? I can bind data to datagrids
and
lists and stuff all day long, but can't seem to grasp this one. Any
pointers
to a useful article / demo (or just any useful pointers!) would be much
appreciated.

Thanks,
Haydn


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top