DataReader to Array

  • Thread starter Thread starter Filip
  • Start date Start date
F

Filip

Hello all,
Is there a way to dump a DataReader into an array, or get the number of rows
so I can get my array sized.

At the moment I'm converting my DataReader into a DataTable so I can get the
size and then reading data from the DataTable into my array.

There has to be better and faster way of doing this.

I don't need and don't really want to use DataAdapter and/or DataSet.

Thanks in advance,
Filip
 
Hi Filip,

Filip said:
Hello all,
Is there a way to dump a DataReader into an array, or get the number of
rows so I can get my array sized.

No, you can't get number of rows.
At the moment I'm converting my DataReader into a DataTable so I can get
the size and then reading data from the DataTable into my array.

There has to be better and faster way of doing this.

I don't need and don't really want to use DataAdapter and/or DataSet.

Why don't you use an ArrayList and convert it to array on the end?
 
Well...you could do it by just adding a transaction for count in front of
your select, not sure why you'd want to though.

string sql = "select count(userid) from users;select username, userid from
users";
SqlCommand cmd = new SqlCommand(sql,cn);
cn.Open();
SqlDataReader rs = cmd.ExecuteReader();
int size=0;
if (rs.Read())
{
size = int.Parse(rs[0].ToString());
}

string[] username = new string[size];
string[] userid = new string[size];

rs.NextResult();

int i=0;
while (rs.Read())
{
username = rs["username"].ToString();
userid = rs["userid"].ToString();
i++;
}
 
Note that the same select will execute twice which might be a performance
hog if the select execution is slow.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

Jerry Fagen said:
Well...you could do it by just adding a transaction for count in front of
your select, not sure why you'd want to though.

string sql = "select count(userid) from users;select username, userid from
users";
SqlCommand cmd = new SqlCommand(sql,cn);
cn.Open();
SqlDataReader rs = cmd.ExecuteReader();
int size=0;
if (rs.Read())
{
size = int.Parse(rs[0].ToString());
}

string[] username = new string[size];
string[] userid = new string[size];

rs.NextResult();

int i=0;
while (rs.Read())
{
username = rs["username"].ToString();
userid = rs["userid"].ToString();
i++;
}


Filip said:
Hello all,
Is there a way to dump a DataReader into an array, or get the number of
rows
so I can get my array sized.

At the moment I'm converting my DataReader into a DataTable so I can get
the
size and then reading data from the DataTable into my array.

There has to be better and faster way of doing this.

I don't need and don't really want to use DataAdapter and/or DataSet.

Thanks in advance,
Filip
 
Note that the same select will execute twice which might be a performance
hog if the select execution is slow.

It might blow up, too, if a new row is added between the two selects.
 
Right, unless a proper transaction is in place.

It would have to be a very "hard" transaction - a "read isolated" one
(my jargon, not proper db term!), which is very expensive (on SQL
Server 2000, anyway - cheaper on Oracle and SQL Server 2005, I
believe).

(Looking at a web site, it's possible I mean "repeatable read". I
sometimes which I had the same sort of level of knowledge about
database concurrency as I do about .NET threading - not expert, but
"good enough". Most of the time I'm happy that I don't have to though
:)
Anyway, reading number of rows beforhand really doesn't make much sense.
Or better, you really don't need to know the row number in advance.

:)
 

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

Back
Top