从MySql到C#数组的查询 [英] A query from MySql to a C# array

查看:194
本文介绍了从MySql到C#数组的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我有个好问题.在Internet的几乎每个部分中,我都能看到一些方法来帮助使用php将Mysql查询保存到数组中,但是我需要使用C#.: suss:
来完成. 我发现了这一点: http://stackoverflow.com/questions/4440378?tab=newest#tab-top [ ^ ]
和我使用ExecuteReader的方法相同,但答案完全没有帮助我:thumbsdown :,我仍然遇到以下问题:

我在mysql中有2列:initialdate,finaldate.以3行数为例.
我执行以下查询:从日期表中选择*
然后,当我想用​​我的C#应用​​程序读取收到的信息时:

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:

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]);
      }
    }



我试图使用类似的方法恢复信息:


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

但这不起作用:((<
如何恢复其余行?嗨!谢谢!



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!

推荐答案

myReader.Read精确读取一行,然后您可以访问列
myReader[0]myReader[1]一起使用,因为只有两列您不能做myreader [2],因为那样会尝试读取您已经说过的不存在的第三列.
只要结果中还剩下行,myReader.Read()(请参见while语句)将返回true.因此,在循环的每次执行中,您都只能处理一行.

希望对您有所帮助! :)

最好的问候,

曼弗雷德(Manfred)
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


这篇关于从MySql到C#数组的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆