简体   繁体   English

如何使用C#从数据表中选择数据库的第一行数据?

[英]How to select the first row data of the database from the data table using C#?

How can I select the first row of the data in the datatable?如何选择数据表中数据的第一行? And display the content on a labelbox, and radiobuttons?并在标签框和单选按钮上显示内容?

OleDbDataAdapter dAdap = new OleDbDataAdapter(command);
DataTable dTable = new DataTable();
dAdap.Fill(dTable);
foreach(DataRow row in dTable.Rows)
{
    question = row["QUESTION"].ToString();
    c1 = row["C1"].ToString();
    c2 = row["C2"].ToString();
    c3 = row["C3"].ToString();
    c4 = row["C4"].ToString();
    ans = row["ANSWER"].ToString(); 
}
lblQuest.Text = question;
btnA.Text = c1;
btnB.Text = c2;
btnC.Text = c3;
btnD.Text = c4;

I tried this code, but it only displays the last row.我试过这段代码,但它只显示最后一行。 Also when I try to rerun the query.此外,当我尝试重新运行查询时。 It didn't refresh/replace/overwrite the values, it only displays the last row of the first query.它没有刷新/替换/覆盖值,它只显示第一个查询的最后一行。

edit:编辑:

i = 0;
x = 0;
lblQuest.Text = dTable.Rows[i].Field<string>(x);
btnA.Text = dTable.Rows[i].Field<string>(x + 1);
btnB.Text = dTable.Rows[i].Field<string>(x + 2);
btnC.Text = dTable.Rows[i].Field<string>(x + 3);
btnD.Text = dTable.Rows[i].Field<string>(x + 4);

是的,因为变量将在每次迭代时被覆盖,而您最终只会拥有最后一次迭代的值

If you want to select only first row values use dTable[0][ColumnIndex]如果您只想选择第一行值,请使用dTable[0][ColumnIndex]

To load next row on button click do:要在按钮上加载下一行,请单击:

int currentRow = 0;
DataTable myDt;

private void LoadNextRow()
{
    currentRow++;
    myLabel.Text = myDt[currentRow]["MyColumn"].ToString();
}

private void OnButtonClick(....)
{
    LoadNextRow();
}

Your issue is that your variables are being overwritten with each loop.您的问题是您的变量被每个循环覆盖。 Once the loop gets through the whole DataTable it ends with the last row and only displays that.一旦循环遍历整个 DataTable,它就会以最后一行结束并且只显示它。

To get the functionality you want you will need to store the DataTable as a property instead of a local variable.要获得您想要的功能,您需要将 DataTable 存储为属性而不是局部变量。

One approach from there is to keep track of the row you are currently viewing.一种方法是跟踪您当前正在查看的行。 Then when the next button is clicked just ++ that current number and reassign the values.然后,当单击下一个按钮时,只需 ++ 那个当前数字并重新分配值。

class YourClass
{
    private DataTable dt { get; set; }
    private int CurrentRow { get; set; }

    private void SomeMethod()
    {
        OleDbDataAdapter dAdap = new OleDbDataAdapter(command);
        dt = new DataTable();
        dAdap.Fill(dt);
        CurrentRow = 0;
        AssignToRow();
    }

    private void AssignToRow()
    {
        var row = dt.Rows[CurrentRow];
        lblQuest.Text = row["QUESTION"].ToString();
        btnA.Text = row["C1"].ToString();
        btnB.Text = row["C2"].ToString();
        btnC.Text = row["C3"].ToString();
        btnD.Text = row["C4"].ToString();
    }

    private void NextButton_Click(object sender, EventArgs e)
    {
        CurrentRow++;
        AssignToRow();
    }
}

暂无
暂无

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

相关问题 如何在单击 C# MVC 中的行中的按钮时将表行中的数据插入数据库 - How can i insert data from a table row into Database on clicking a button in the row in C# MVC 如何使用C#来让用户选择mysql数据库中的特定列和行? - How to let the user select a specific column and row in mysql database using c# to retrieve data? 使用实体框架 C# 从数据库表中删除行数据 - Delete Row data from database table using Entity Framework C# 如何使用 C# 从 SQL Server 数据库中逐行检索数据到 datagridview - How to retrieve data into a datagridview row by row from a SQL Server database using C# 如何从表1中选择数据并将其插入PostgresQLusing C#中的表2中 - How to Select data from table 1 and insert into table 2 in PostgresQLusing C# 从数据库中选择数据,使用c#参数 - select data from database, using c# parameter for it 使用2 DataGridView C#WindowsForm将数据插入表(从选择) - Insert data into table(from select) using 2 DataGridView c# windowsForm 如何使用C#将一行数据从一个Access数据库正确复制到另一个数据库? - How to properly copy a row of data from one Access database to another using C#? C#:如何从数据库的最后一行获取数据? 我在Visual Studio 2017中使用 - C#: How to get data from the last row in database? I'm using in visual studio 2017 如何在Visual Studio中从数据库中获取数据时使HTML行可单击(使用C#) - How to make an HTML row clickable while fetching data from the database (Using C#) in visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM