1
MySqlCommand cmd = new MySqlCommand("select id from table", conn);
MySqlDataReader dr = cmd.ExecuteReader();

for example i get 4 row, so 23,3,12,9

how i can get array this integer?

int[] arr = { 23, 3, 12, 9 };

2 Answers 2

2

Use this:

IEnumerable<int> r;
using (reader)
{
    r = Read(reader);
}
int[] arr = r.ToArray();

where you can implement the method in various ways:

private static IEnumerable<int> Read(IDataReader reader)
{
    IList<int> list = new List<int>();
    while (reader.Read())
    {
        list.Add(reader.GetInt32(0));
    }
    return list;
}

or (extending Aghilas's answer):

private static IEnumerable<int> Read(IDataReader reader)
{
    while (reader.Read())
    {
        yield return reader.GetInt32(0);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

you can use yield iterator

while(reader.Read()
{
    yield return reader.GetInt32(0);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.