469,268 Members | 962 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

How to put database results into an array?

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
Nov 18 '05 #1
4 4829
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[i][j]);
}
}

Karl

"Haydnw" <ha****@removeyahooremove.com> wrote in message
news:OY*************@tk2msftngp13.phx.gbl...
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

Nov 18 '05 #2
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:ed*************@TK2MSFTNGP12.phx.gbl...
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[i][j]);
}
}

Karl

"Haydnw" <ha****@removeyahooremove.com> wrote in message
news:OY*************@tk2msftngp13.phx.gbl...
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


Nov 18 '05 #3
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" <ha****@removeyahooremove.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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:ed*************@TK2MSFTNGP12.phx.gbl...
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[i][j]);
}
}

Karl

"Haydnw" <ha****@removeyahooremove.com> wrote in message
news:OY*************@tk2msftngp13.phx.gbl...
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



Nov 18 '05 #4
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" <none> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
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" <ha****@removeyahooremove.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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:ed*************@TK2MSFTNGP12.phx.gbl... 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[i][j]);
}
}

Karl

"Haydnw" <ha****@removeyahooremove.com> wrote in message
news:OY*************@tk2msftngp13.phx.gbl...
> 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
>
>



Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by richard.pasco | last post: by
reply views Thread by XML newbie: Urgent pls help! | last post: by
3 posts views Thread by josh.kuo | last post: by
reply views Thread by windandwaves | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.