本文介绍了C#如何在SQL中显示文本框中的下一个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是,我有多个用户使用相同的名字和姓氏。可以推动下一个按钮并在文本框中显示。



我的搜索代码工作得很好但是下一步不行。



我尝试过:



i want wen i have more than 1 user whith the same firstname and lastname. the possibility to push next buttton and show in textbox.

my search code work great but nextresult dont work.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace recherchenext
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
          
            SqlConnection connection = new SqlConnection("Data Source=94187H2\\SQLEXPRESS;Integrated Security=TRUE;Initial Catalog=RSE");
            SqlCommand command;          
            SqlDataReader Reader;
            string selectQuery = "select * from CLIENT where Nom  =  '" + tbFirstNameS.Text + "'and Prenom = '" + tbLastNameS.Text + "'";
            connection.Open();
            command = new SqlCommand(selectQuery, connection);
            Reader = command.ExecuteReader();
          

           
                if (Reader.Read())
                    tbNom.Text = Reader["nom"].ToString();
                    tbPrenom.Text = Reader["Prenom"].ToString();
                    tbTelephone.Text = Reader["Telephone"].ToString();
            
            if (Reader.NextResult())
                tbNom.Text = Reader["nom"].ToString();
                tbPrenom.Text = Reader["Prenom"].ToString();
                tbTelephone.Text = Reader["Telephone"].ToString();

            connection.Close();


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btNext_Click(object sender, EventArgs e)
        {

        }
    }
        }

推荐答案

//load data into that object
DataTable myData;
//this identify your current record
int currentRecord = 0;
private void btNext_Click(object sender, EventArgs e)
{
    var oneDataRow = myData.AsEnumerable()
        .Skip(currentRecord)
        .Take(1);
    //TextBox1.Text = oneDataRow[0];
    currentRecord+=1;
}





详情请见:

[ ]

[]

[]



但是(!)最后,我建议阅读有关分页的内容:

[]

[ []

[]



For further details, please see:
Enumerable.Skip(TSource) Method (IEnumerable(TSource), Int32) (System.Linq)[^]
Enumerable.Take(TSource) Method (IEnumerable(TSource), Int32) (System.Linq)[^]
101 LINQ Samples in C#[^]

But(!) finally, i'd suggest to read about pagination:
Tutorial 24: Paging and Sorting Report Data[^]
Tutorial 25: Efficiently Paging Through Large Amounts of Data[^]
Walkthrough: Displaying, Paging, and Sorting Data Using the ListView Web Server Control[^]
Paging Through a Query Result | Microsoft Docs[^]


这篇关于C#如何在SQL中显示文本框中的下一个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 10:13