Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello people.
I have a good question. In almost every part of Internet I see methods that help to save Mysql queries into an array using php, BUT I need to do it using C#.:suss:
I found this: http://stackoverflow.com/questions/4440378?tab=newest#tab-top[^]
and I have the same method using ExecuteReader but the answer didn't help me at all :thumbsdown:, I still have the following problem:

I have 2 columns in mysql: initialdate, finaldate. With 3 numbers of rows as an example.
I execute the following query: select * from datetable
And then, when I want to read the received information with my C# app:

C#
myReader = myCommand.ExecuteReader();
      while(myReader.Read())
      {
        //it recovers the first value of first row
        Console.WriteLine(myReader.GetString(0));
        //it recovers the second value of first row
        Console.WriteLine(myReader.GetString(1));
        //I can't recover the rest of info. If I execute the next line
        //an error of 'index value out of range' or something similar
        Console.WriteLine(myReader.GetString(2));

        //this part works in the same way. I use it.
        //(and the problem is still the same too):
        Console.WriteLine(myReader[0]);
        //works good
        Console.WriteLine(myReader[1]);
        //ouch:
        Console.WriteLine(myReader[2]);
      }
    }


I tried to recover the info using something like:


Console.WriteLine(myReader.GetString(0,0));
//or:
Console.WriteLine(myReader[0,0]);
Console.WriteLine(myReader[0][0]);

But it doesn't work :((

How can I recover the rest of rows? HEEEELP! and Thanks!
Posted

1 solution

myReader.Readreads exactly one row and then you can access the columns
with myReader[0] and myReader[1] because there are only two columns you cant do myreader[2] as that would try to read the third columns which as you already stated doesn't exist.
As long as there are rows left in your result the myReader.Read() (see your while statement) will return true. So in every execution of your loop you're able to process exactly one line.

Hope that helps! :)

Best regards,

Manfred
 
Share this answer
 
v3
Comments
pancho2413 29-Dec-10 14:50pm    
You are so amazing...
no other words in my mouth:

I'LL THANK YOU 'TIL I DIE
fjdiewornncalwe 29-Dec-10 16:12pm    
Great answer... Not sure who would univote you on that one...
Manfred Rudolf Bihy 29-Dec-10 16:20pm    
@Marcus: Yes I know, there are all kinds of strange folk hanging out in this place. At least OP was satisfied :).
Thanks for the upvote!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900