array

  • Thread starter Thread starter frazer
  • Start date Start date
F

frazer

hi i am trying to use string arrays to store values from the db

string users="";
while (sqlDataReader.Read())
{
users = users+ sqlDataReader.GetString(1) + ",";
}
string [] users1 = users.Split(',');


rather than first use a string to store the roles and then put them in a
string array
is there a way to straight away put them in an array.thnx
 
frazer said:
hi i am trying to use string arrays to store values from the db

string users="";
while (sqlDataReader.Read())
{
users = users+ sqlDataReader.GetString(1) + ",";
}
string [] users1 = users.Split(',');

rather than first use a string to store the roles and then put them in a
string array
is there a way to straight away put them in an array.thnx

You can use ArrayList then to ToArray, or if you know how many elements are
going to be in the array, then you can just declare it at the beginning:

//ArrayList
ArrayList arr = new ArrayList();
//...
arr.Add(users + /*...*/);

string [] strs = (string[])arr.ToArray(typeof(string));

//known size
string[] strs = new string[num_elements];
//...
strs[index++] = users + /*...*/;
 
Hello

If you know the number of records returned, you can allocate the array
before the loop.
You can know that with a "SELECT COUNT(*) FROM mytable", but you have to be
use locking or transactions because another process/thread may add or delete
records to the table
between the record count query and the loop.

Otherwise you can use ArrayList or StringCollection class.
If you must put in an array, you can use ArrayList's to array method.

Best regards,
Sherif
 

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

Similar Threads

MVC Web-API - token based authentication 2
Problem with ArrayList 4
Shape Arrays VBA 0
Search Array 17
Dis-arrayed arrays 2
Combining 2 string arrays 5
How array allocation is implemented in c#? 1
JSON.Net 6

Back
Top