C# 如何显示mysql中的数据并逐个显示?

C# 如何显示mysql中的数据并逐个显示?,c#,mysql,C#,Mysql,我在mysql中有一个数据,我希望每次单击按钮时都逐个显示数据。怎么做 字符串ConnectToServer=@“server=*。;port=***;user id=sampleID;password=samplePW;database=sampleDB;pooling=false” 输出: Name1 Name2 Name3 Name4 Name5 但是我不想,每次我点击按钮,名称将一个接一个出现 像这样: click = output Name1 click = output Name2

我在mysql中有一个数据,我希望每次单击按钮时都逐个显示数据。怎么做

字符串ConnectToServer=@“server=*。;port=***;user id=sampleID;password=samplePW;database=sampleDB;pooling=false”

输出:

Name1
Name2
Name3
Name4
Name5
但是我不想,每次我点击按钮,
名称将一个接一个出现

像这样:

click = output Name1
click = output Name2
click = output Name3
click = output Name4
click = output Name5

您正在读取所有记录:

while (NameReader.Read())
如果只想读取一个,请尝试将所有连接置于该方法之外并运行

NameReader = NameCommand.ExecuteReader();
只有一次

然后改变

while (NameReader.Read())


根据您需要数据的实时性以及您希望进行多少DB调用,至少有两种方法可以做到这一点。这是:

  • 选项1
  • 初始化名称列表和索引变量的类级别变量

    List<string> names = null;
    int currentNameIndex = 0;
    
    疑问是

    Select TOP 1 nameId, names from SampleNames Where NameId > currentNameId Order By NameId;
    
    currentNameId = int.Parse(NameReader[nameId].ToString());
    
    上面的查询假设nameId是唯一的键,值从0开始或大于-1,并且它们是增量的。(身份证等)

    正如我提到的,如果你能提供表结构,我们可以回答得更好

    选项#1在DB调用中很有效,但可能有过时的数据。
    选项#2比选项#1更健谈,但实时数据更多。

    非常感谢,我使用了选项#1,它工作了,很高兴它工作了。对于结束语,请将其标记为答案。
    List<string> names = null;
    int currentNameIndex = 0;
    
    private void ButtonName_Click(object sender, EventArgs e)
    {
        if (names == null)
        {
         names = GetNames();
        }
    
        if (currentNameIndex < names.Count)
        {
         NameLabel.Text += names[currentNameIndex++];
        }    
    }
    
    int currentNameId = -1;  // class level variable.
    
    Select TOP 1 nameId, names from SampleNames Where NameId > currentNameId Order By NameId;
    
    currentNameId = int.Parse(NameReader[nameId].ToString());