gpt4 book ai didi

c# - 如何通过迭代行从 DataTable 中检索数据

转载 作者:行者123 更新时间:2023-11-29 18:08:38 25 4
gpt4 key购买 nike

我想将其放入循环中,代码工作正常但我无法为其循环。当它检测到表上有新数据时,它会自动添加另一个项目,在我下面的代码中仅显示两个用户控件,但我需要生成表中的所有值。这是代码

public partial class Form1 : Form
{
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;database=rmsdb;username=root;password=");
MySqlCommand command;
MySqlDataAdapter da;
public Form1()
{
InitializeComponent();
LRNincrement();

}
int poss = 10;
public void AddItems(string Text, bool Checked)
{
DynaItems item = new DynamicUserControl.DynaItems(Text, Checked);
PanelContainer.Controls.Add(item);
item.Top = poss;
poss = (item.Top + item.Height + 10);
}

private void additembtn_Click(object sender, EventArgs e)
{
LRNincrement();
txt.Text = "";
}
private void LRNincrement()
{
String selectQuery = "SELECT lrnnumber from rmsdb.studentstable";
command = new MySqlCommand(selectQuery, connection);

da = new MySqlDataAdapter(command);

DataTable table = new DataTable();

da.Fill(table);
if (table.Rows.Count > 0)
{
string trylol = table.Rows[0][0].ToString();
AddItems(trylol, true);
}
if (table.Rows.Count > 1)
{
string trylol1 = table.Rows[1][0].ToString();
AddItems(trylol1, true);
}
}
}

最佳答案

替换下面的代码

        {
string trylol = table.Rows[0][0].ToString();
AddItems(trylol, true);
}
if (table.Rows.Count > 1)
{
string trylol1 = table.Rows[1][0].ToString();
AddItems(trylol1, true);
}

    string itemName = string.Empty;

for(int i = 0; i< table.Rows.Count; i++)
{
itemName = table.Rows[i][0].ToString();
AddItems(itemName, true);
}

我无法测试此代码。但希望这可以帮助您找到解决方案。

关于c# - 如何通过迭代行从 DataTable 中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47597263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com