简体   繁体   English

MySQL阅读器用法C#

[英]MySQL reader usage C#

Ive been having some problems with understanding the MySql reader from MySql.Data.MySqlClient 从MySql.Data.MySqlClient了解MySql阅读器时遇到了一些问题

This is my Code: 这是我的代码:

while (reader.Read())
{
    documentButtons = new RadioButton[3];
    for (int i = 0; i < 3; i++)
    {
        documentButtons[i] = new RadioButton();
        documentButtons[i].Text = reader["name"].ToString() + " " + (i + 1);

        Console.WriteLine(reader["name"].ToString());

        documentButtons[i].Location = new System.Drawing.Point(10, 10 + i * 20);
        this.Controls.Add(documentButtons[i]);
    }
}
Console.ReadLine();

This makes 3 radiobuttons with the texts: "Dokument1 1", "Dokument1 2", "Document1 3" As you can probably see, i want the number right behind "Dokument" to be equal to the number behind that. 这使3个单选按钮的文本如下:“ Dokument1 1”,“ Dokument1 2”,“ Document1 3”如您可能看到的,我希望“ Dokument”后面的数字等于其后的数字。 (My 3 first entries in the database is "Dokument1", "Dokument2" and "Dokument3". In my console, "Dokument1", 2 and 3, all show up 3 times. How can i make it create the appropriate name on the appropriate radiobutton? (我在数据库中的前3个条目是“ Dokument1”,“ Dokument2”和“ Dokument3”。在我的控制台中,“ Dokument1”,2和3都显示3次。如何使它在适当的单选按钮?

I'm not sure I fully understand your question, but if you simply want to have a single RadioButton for each row returned, the following code should work: 我不确定我是否完全理解您的问题,但是如果您只是想为返回的每一行都使用一个RadioButton,那么以下代码应该可以工作:

documentButtons = new RadioButton[3];
int i = 0;
while (i <= 3 && reader.Read()) { // make sure we don't get an IndexOutOfRangeException
  documentButtons[i] = new RadioButton();
  documentButtons[i].Text = reader["name"].ToString() + " " + (i + 1);

  Console.WriteLine(reader["name"].ToString());

  documentButtons[i].Location = new System.Drawing.Point(10, 10 + i * 20);
  this.Controls.Add(documentButtons[i]);

  ++i;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM