One thing you can do is create a generic method that takes the parameters that differ. For example:
public static ArrayList LoadInfo<T>(string sql, Func<SqlDataReader, T> getItem) where T: new
{
var list = new ArrayList();
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
list.Add(getItem(dr));
}
return list;
}
public static User LoadUser(SqlDataReader dr)
{
User structure_A = new User();
structure_A.username = dr.GetValue(0).ToString();
structure_A.status = dr.GetValue(1).ToString();
return User;
}
Now, to call it to load users, you just pass it the SQL query and the LoadUser
method:
string usersQuery = "SELECT ...."; // select users query
ArrayList usersList = LoadInfo(usersQuery, LoadUser);
And if you need to load a bunch of Fooby
objects, you'd create the Fooby query and a LoadFooby
method that can create and populate a Fooby from the data row.
I'd suggest that you look into using List rather than ArrayList
.