I'd like to populate an User array with data from my SQL Server database using a SqlDataReader.
This is my code so far:
public struct User
{
public int id;
public string log;
public string password;
public User (int id1,string s, s2)
{
id=id1;
log =s;
password=s2;
}
}
User[] al = new User[50];
int i=0;
using (SqlConnection connection = new SqlConnection("string")
{
connection.Open();
SqlCommand command = new SqlCommand("Select [UserName], [Password]. from [TaUser]", connection);
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// populate the al array with the datas from the 3 columns : ID, UserName, Password
}
}
connection.Close();
}
I know that if I had a simple arraylist I could just do al.Add(""), however, I struggle when it comes to struct arrays.
List<User>with.Addand then call.ToArray()to get array from listUserNamefield in the struct, for example; instead it has a mysterious field namedlog. Is this intentional? Also, your query returns two columns, but the constructor forUserrequires 3 arguments. What is going on here?