简体   繁体   English

使用C#在文本框的foreach或while循环中显示行来遍历MySql数据库

[英]Iterate through MySql database using C# displaying rows in textbox's foreach or while loop

I'm having a little trouble with my application, I have a Database connected and have already displays the values from the first row in the database, however I need to have the database iterate through each row and display them accordingly replacing what was previously there and ideally being delayed by a timer 我的应用程序遇到了一些麻烦,已经连接了数据库,并且已经显示了数据库第一行中的值,但是我需要让数据库遍历每一行并相应地显示它们,以替换以前的内容并最好由计时器延迟

I thought this was the correct way of going about it however it doesn't seem right as now it doesn't display anything and gives me a System.NullReferenceException. 我以为这是正确的解决方法,但是它似乎不正确,因为它现在不显示任何内容,并给我System.NullReferenceException。 I have tried to solve the issue but seems to be having no luck. 我试图解决这个问题,但似乎没有运气。 If you could recommend something to read, that would be greatly helpful just can't see a lot on the internet for my specific problem 如果您可以推荐阅读的内容,那将非常有帮助,只是对于我的特定问题在互联网上看不到太多

Any help will be great first time posting on here so sorry if things are in the wrong format. 任何帮助都会是第一次在这里发布的绝佳帮助,如果格式错误,抱歉。

    private void FillInTextFields(DataTable table, int ind)
    {
        foreach(DataRow dr in table.Rows)
        {
            foreach(var item in dataRow.ItemArray)
            {
                dataRow = table.Rows[ind];
                txtNHSNumber.Text = dataRow.ItemArray.GetValue(0).ToString();
                txtFirstName.Text = dataRow.ItemArray.GetValue(1).ToString();
                txtLastName.Text = dataRow.ItemArray.GetValue(2).ToString();
                txtTimeDate.Text = dataRow.ItemArray.GetValue(3).ToString();
                txtHeartRate.Text = dataRow.ItemArray.GetValue(4).ToString();
                txtTemp.Text = dataRow.ItemArray.GetValue(5).ToString();
                txtReps.Text = dataRow.ItemArray.GetValue(6).ToString();
                txtDia.Text = dataRow.ItemArray.GetValue(7).ToString();
                txtSys.Text = dataRow.ItemArray.GetValue(8).ToString();
            }
        }
    }
foreach(var item in dataRow.ItemArray)

should be: 应该:

foreach(var item in dr.ItemArray)

can put a sleep at the end of the loop to delay before going back to the top. 可以在循环结束时进行睡眠,以延迟返回顶部之前的时间。

It seems you have a few issues. 看来您有一些问题。 First, your inner loop doesn't seem to be necessary. 首先,您的内循环似乎不是必需的。 Second, the outer loop is going through all the rows, even though you're specifically passing the row index into your method. 其次,即使您特别将行索引传递到方法中,外部循环也会遍历所有行。 Finally, it seems you're using your "dataRow" variable before it's assigned, although I'd have to see more code to be sure. 最后,似乎您在分配变量之前正在使用“ dataRow”变量,尽管我必须确保看到更多代码。 Consider this: 考虑一下:

private void FillInTextFields(DataTable table, int ind)
{
    dataRow = table.Rows[ind];
    txtNHSNumber.Text = dataRow.ItemArray.GetValue(0).ToString();
    txtFirstName.Text = dataRow.ItemArray.GetValue(1).ToString();
    txtLastName.Text = dataRow.ItemArray.GetValue(2).ToString();
    txtTimeDate.Text = dataRow.ItemArray.GetValue(3).ToString();
    txtHeartRate.Text = dataRow.ItemArray.GetValue(4).ToString();
    txtTemp.Text = dataRow.ItemArray.GetValue(5).ToString();
    txtReps.Text = dataRow.ItemArray.GetValue(6).ToString();
    txtDia.Text = dataRow.ItemArray.GetValue(7).ToString();
    txtSys.Text = dataRow.ItemArray.GetValue(8).ToString();
}

You'll just need to call this method with the particular index you want to display. 您只需要使用要显示的特定索引来调用此方法。

You can also remove the ItemArray.GetValue stuff, and just go with this: 您还可以删除ItemArray.GetValue东西,然后执行以下操作:

private void FillInTextFields(DataTable table, int ind)
{
    dataRow = table.Rows[ind];
    txtNHSNumber.Text = dataRow[0].ToString();
    txtFirstName.Text = dataRow[1].ToString();
    txtLastName.Text = dataRow[2].ToString();
    txtTimeDate.Text = dataRow[3].ToString();
    txtHeartRate.Text = dataRow[4].ToString();
    txtTemp.Text = dataRow[5].ToString();
    txtReps.Text = dataRow[6].ToString();
    txtDia.Text = dataRow[7].ToString();
    txtSys.Text = dataRow[8].ToString();
}

Not sure why you are passing int ind in this method it's not needed. 不知道为什么不用这种方法传递int ind了。

private void FillInTextFields(DataTable table)
{
    txtNHSNumber.Text = table.Rows[0].ItemArray[0];
    txtFirstName.Text = table.Rows[0].ItemArray[1];
    txtLastName.Text = table.Rows[0].ItemArray[2];
    txtTimeDate.Text = table.Rows[0].ItemArray[3];
    txtHeartRate.Text = table.Rows[0].ItemArray[4];
    txtTemp.Text = table.Rows[0].ItemArray[5];
    txtReps.Text = table.Rows[0].ItemArray[6];
    txtDia.Text = table.Rows[0].ItemArray[7];
    txtSys.Text = table.Rows[0].ItemArray[8];
}

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

相关问题 在 c# Selenium 中使用 for 循环遍历行和单元格 - iterate through rows and cells using for loop in c# Selenium 遍历foreach循环-处理C#需要很长时间 - iterate through foreach loop - takes long for processing c# C#如何在Foreach循环中遍历表单控件 - How does C# iterate through Form Controls in a Foreach loop 在文本框C#中的不同行上显示来自数据库的值 - Displaying values from a database on different rows in a textbox C# 使用Silverlight / c#wpf遍历/循环Datagrid行以获取复选框列的值:NullReferenceException错误 - Iterate/Loop through Datagrid rows to get the value of checkbox column using Silverlight/ c# wpf: NullReferenceException error C# - 使用 foreach 和 if 语句循环遍历 DataGridView 行 - 缺少最后一个条目 - C# - Loop through DataGridView Rows using foreach and if statement - Missing last entry 使用foreach循环C#将数组的内容写入文本框 - Writing content of an array to a textbox using foreach loop C# C#通过foreach循环更新数据库。 错误 - C# Update database through foreach loop. ERROR Foreach标记可遍历c#对象并使用Entity Framework插入数据库 - Foreach tag to loop through c# object and insert to database using Entity Framework 使用 foreach 循环遍历两个列表 - Using foreach loop to iterate through two lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM