【问题标题】:Reading with MySqlDataReader使用 MySqlDataReader 阅读
【发布时间】:2011-04-09 17:33:58
【问题描述】:

您好,我在阅读 MySqlDataReader 时遇到问题。我试图将while() 更改为if(),然后它就起作用了。所以我对while (Reader.Read()) 做错了。感谢您的回答。 (今天的另一个问题已解决,评论的人帮助了我xd)

using (MySqlCommand cmd = new MySqlCommand
      ("SELECT * FROM `citationer`",  mysqlCon))
{
    try
    {
        MySqlDataReader Reader = cmd.ExecuteReader();
        while (Reader.Read()) // this part is wrong somehow
        {
            citationstexter.Add(Reader.GetString(loopReading)); // this works
            loopReading++; // this works
        }
        Reader.Close();
    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

【问题讨论】:

  • 您能否提供异常详细信息。我觉得很好。

标签: c# mysql connector


【解决方案1】:

您的问题是loopReading 参数对GetString 的使用。 此参数应该是从零开始的列序号(列号),但是您要为读取的每一行递增它。

查看此处了解更多信息: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getstring.aspx

您应该将 0 用于引用表的第一列,将 1 用于第二列,等等。

另外,使用

  using(MySqlDataReader Reader = cmd.ExecuteReader()) 
  {
    ...
  }

就像你为 mySqlCommand 对象所做的那样来保存内存泄漏(但这不是你的问题。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    相关资源
    最近更新 更多