C# C-跳到数据库中的ID

C# C-跳到数据库中的ID,c#,database,C#,Database,我就要放弃了。连续5个小时尝试了很多东西。我什么也没想到 我有一个C应用程序,可以添加记录,从数据库中删除记录。它还可以在文本框中显示项目,我可以使用“下一步”和“上一步”按钮浏览项目。例如下面的“下一步”按钮 private void btnNext_Click(object sender, EventArgs e) { if (inc != MaxRows - 1) { inc++; Naviga

我就要放弃了。连续5个小时尝试了很多东西。我什么也没想到

我有一个C应用程序,可以添加记录,从数据库中删除记录。它还可以在文本框中显示项目,我可以使用“下一步”和“上一步”按钮浏览项目。例如下面的“下一步”按钮

    private void btnNext_Click(object sender, EventArgs e)
    {
        if (inc != MaxRows - 1)
        {
            inc++;
            NavigateRecords();
        }
        else
        {
            MessageBox.Show("You have reached the end of available items", "End of Available Items", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
麦克斯罗是这个吗

MaxRows = ds1.Tables["Laptops"].Rows.Count;
我在上面调用的方法是NavigateRecords,下面是代码

private void NavigateRecords()
    {
        DataRow dRow = ds1.Tables["Laptops"].Rows[inc];

        txtMaker.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(1).ToString();
        txtModel.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(2).ToString();
        txtPrice.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(3).ToString();
        txtBids.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(4).ToString();
        txtScreen.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(5).ToString();
        txtCPU.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(6).ToString();
        txtMemory.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(7).ToString();
        txtHD.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(8).ToString();
        picLaptops.Image = Image.FromFile(ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(9).ToString());
    }
我的Access数据库中有一个自动编号ID字段,用户可以通过在文本框中输入ID来跳过该字段,并在不同的文本框中显示相应的记录。请参阅导航记录

我的问题是,我该怎么做?我需要在NavigateRecords的文本框中显示正确的行,该行还将更新全局设置为0的inc变量。例如,如果用户启动程序并跳过说id 7,它将显示该记录,但当它这样做时,inc还应更新行号,以便如果我单击“下一步”,它将带我到id 8,而不是id 2。。。如果这有道理的话。到目前为止,我有这个

private void btnSkip_Click(object sender, EventArgs e)
    {
        string searchFor = txtSkip.Text;
        DataRow[] Skip;
        int results = 0;

        Skip = ds1.Tables["Laptops"].Select("ID='" + searchFor + "'");

        results = Skip.Length;

        if (results > 0)
        {
            DataRow dr1;

            for (int i = 0; i < MaxRows; i++)
            {
                // who knows what to do here.... or getting every row is even the right thing to do...

            }

        }
        else
        {
            MessageBox.Show("No item found");
        }
    }

修改第二个代码示例:

private void btnSkip_Click(object sender, EventArgs e)
{
    string searchFor = txtSkip.Text;
    DataRow[] target = ds1.Tables["Laptops"].Select("ID='" + searchFor + "'");
    // I suspect that this should really not be a string, so ("ID=" + searchFor);

    if (target.Count() == 1) {
        inc = ds1.Tables["Laptops"].Rows.IndexOf(target[0]);
        NavigateRecords();
    } else { //there can be no more than one row, so this means no matches
        MessageBox.Show("No item found");
    }
}

…这是假设ID是表中的唯一标识符。

修改第二个代码示例:

private void btnSkip_Click(object sender, EventArgs e)
{
    string searchFor = txtSkip.Text;
    DataRow[] target = ds1.Tables["Laptops"].Select("ID='" + searchFor + "'");
    // I suspect that this should really not be a string, so ("ID=" + searchFor);

    if (target.Count() == 1) {
        inc = ds1.Tables["Laptops"].Rows.IndexOf(target[0]);
        NavigateRecords();
    } else { //there can be no more than one row, so this means no matches
        MessageBox.Show("No item found");
    }
}

…这是假设ID是表中唯一的标识符。

仔细查看一下代码,我觉得您可能应该在for循环中调用NavigateRecords


此外,如果您刚刚开始UI开发,您可能希望了解MVVM设计模式,尤其是在您使用WPF时。如果您使用WinForms或其他东西,那么MVP模式可能值得一看。无论采用哪种方式,这些模式都有助于将演示文稿与应用程序逻辑分离,并可能简化类似的问题。

仔细查看一下您的代码,在我看来,您可能应该在for循环中调用NavigateRecords


此外,如果您刚刚开始UI开发,您可能希望了解MVVM设计模式,尤其是在您使用WPF时。如果您使用WinForms或其他东西,那么MVP模式可能值得一看。无论哪种方式,这些模式都有助于将表示与应用程序逻辑分离,并可能简化类似的问题。

如果id是唯一的,则您知道Select方法将只返回一行。这个问题在SQL中很容易解决,您可以简单地从笔记本电脑中选择TOP 1,其中id>@currentID ORDER BY id ASC用于下一个,从笔记本电脑中选择TOP 1,其中id0且索引@currentID ORDER BY id ASC用于下一个,从笔记本电脑中选择TOP 1,其中id0且索引