2011-11-16 41 views
0

的Ive有一些问题从MySql.Data.MySqlClientMySQL的读者使用C#

了解MySQL的读者这是我的代码:

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(); 

这使得3个单选按钮与文本:“Dokument1 1 “,”Dokument1 2“,”Document1 3“ 正如你可能看到的,我希望”Dokument“后面的数字等于后面的数字。 (我的3个数据库中的第一个条目是“Dokument1”,“Dokument2”和“Dokument3”。在我的控制台中,“Dokument1”,2和3都显示了3次,我怎样才能使它创建合适的名称适当的单选按钮

+0

为什么for循环在那里,如果你想要的只是三个单选按钮?你正在为每个数据库记录创建三个,它看起来像。 – reallyJim

回答

4

我不知道我完全理解你的问题,但如果你只是想有一个单选按钮返回的每个行,下面的代码应该工作:

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; 
} 
+0

我喜欢你刚刚发布的几乎是字母的答案,我正在输入:) – reallyJim

+1

@ reallyJim:你必须输入** F ASTER **! ;) – knittl

+0

啊,这是与读者和我一直在寻找的“我”,同时感谢很多:) 像一个魅力工程谢谢你:D – Raptor