By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,831 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,831 IT Pros & Developers. It's quick & easy.

Build a dynamic array from DataReader...

P: n/a
I'm having a hard time wrapping my head around how to build a
multi-dimensional array of n length out of a DataReader loop. Take this
pseudo-code:

=======================================
public string[,] get_array(string sql)
{

//create db connection & open

string[,] arrReturn = new String[,];

SqlDataReader dataReader = myCommand.ExecuteReader();

if (dataReader.HasRows)
{
while (dataReader.Read())
{
//next line I don't know what to do!
arrReturn.AddToArray = {dataReader[0].ToString(),
dataReader[1].ToString()};
}
}

myConn.Close();
return arrReturn;
}
========================================

I can't seem to find anything in my Google searches that relates to
this. ArrayList seems only to work for unidimensional arrays, while
Array.CreateInstance requires that I know the array size before
populating it (which a datareader can't give me).

I'd sure appreciate any help!

--Brent
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
a jagged (array of arrays) might make more sense. easiest is an araylist of
string arrays.

//create db connection & open

ArrayList al = new ArrayList();

SqlDataReader dataReader = myCommand.ExecuteReader();

if (dataReader.HasRows)
{
while (dataReader.Read())
{
string[] fields = new string[datareader.FieldCount];
for (int i =0; i < datareader.FieldCount; ++i)
fields[i] = dataReader[i].ToString() ;
al .Add(fields);
}
}
string [][] arrReturn = (string[][]) al .ToArray(string[]);

//print
for (int row=0; row<arrReturn.Count; ++ row)
for (int col=0; col < arrReturn[row].Length; ++col)
print (arrReturn[row][col]);
-- bruce (sqlwork.com)


"Brent" <""b b i g l e r \"@ y a h o o . c o m"> wrote in message
news:11*************@corp.supernews.com...
I'm having a hard time wrapping my head around how to build a
multi-dimensional array of n length out of a DataReader loop. Take this
pseudo-code:

=======================================
public string[,] get_array(string sql)
{

//create db connection & open

string[,] arrReturn = new String[,];

SqlDataReader dataReader = myCommand.ExecuteReader();

if (dataReader.HasRows)
{
while (dataReader.Read())
{
//next line I don't know what to do!
arrReturn.AddToArray = {dataReader[0].ToString(),
dataReader[1].ToString()};
}
}

myConn.Close();
return arrReturn;
}
========================================

I can't seem to find anything in my Google searches that relates to this.
ArrayList seems only to work for unidimensional arrays, while
Array.CreateInstance requires that I know the array size before populating
it (which a datareader can't give me).

I'd sure appreciate any help!

--Brent

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.