C#延迟SQL查询,而在一个文本框输入用户 [英] C# Delay SQL Query while user typing in a textbox

查看:99
本文介绍了C#延迟SQL查询,而在一个文本框输入用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚写了与用户写入一个文本框的信息查询SQL数据库我的第一个程序。这是使用Windows窗体C#。我的目标是有这种类似的地方结果显示为一个用户类型(类似于谷歌的预测搜索功能)在我们的ERP软件的搜索功能。

I have just written my first program that queries a SQL database with the information a user writes into a textbox. This is C# using windows forms. My goal is to have this be similar to the search function in our ERP software where results are displayed as a user types (similar to Google's prediction search feature).

我我与正在减少的查询数量到数据库中挣扎。现在我有这么不执行查询,直到用户类型为至少3个字符,否则过多的结果将被返回。

What I am struggling with is reducing the number of queries to the database. Right now I have it so that a query is not executed until the user has typed as least 3 characters otherwise too many results would be returned.

private void SearchField_TextChanged(object sender, EventArgs e)
{
    string search = SearchField.Text;
    if (search.Length >= 3)
    {
        dataGridView1.DataSource = sql.ExecuteQuery(Query(search));
    }
}



我想补充的是查询延迟,直到用户已经停止了打字,而没有进入这么多毫秒的字符。我一直在寻找的定时器类,但与我找到正确实现它的例子中挣扎。基本上,我想改变我的代码是类似以下内容:

What I want to add is a query delay until the user has stopped typing or rather not entered a character for so many milliseconds. I have been looking at the timer class but struggling with the examples I found to implement it properly. Basically I want to change my code to be something like the following:

private void SearchField_TextChanged(object sender, EventArgs e)
{
    string search = SearchField.Text;
    if (search.Length >= 3 && aTimer.Time > 500)  //500 is milliseconds
    {
        dataGridView1.DataSource = sql.ExecuteQuery(Query(search));
    }
    aTimer.Reset();
}

如果使用定时器类,我不知道如何正确地执行它。如果有一个更好的解决方案,我是持开放的态度为好。

If using the timer class I don't know how to implement it properly. If there is a better solution I would be open to that as well.

推荐答案

您想要做的是安排查询,在未来的某个时刻发生,同时能够重置或撤销挂起查询作为用户类型。下面是一个例子:

What you want to do is schedule the query to happen at some point in the future, while being able to reset or revoke the pending query as the user types. Here is an example:

using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    Timer queryTimer;
    TextBox SearchField;

    public Form1()
    {
        Controls.Add((SearchField = new TextBox { Location = new Point(10, 10) }));
        SearchField.TextChanged += new EventHandler(SearchField_TextChanged);
    }

    void SearchField_TextChanged(object sender, EventArgs e)
    {
        if (SearchField.Text.Length < 3)
            RevokeQueryTimer();
        else
            RestartQueryTimer();
    }

    void RevokeQueryTimer()
    {
        if (queryTimer != null)
        {
            queryTimer.Stop();
            queryTimer.Tick -= queryTimer_Tick;
            queryTimer = null;
        }
    }

    void RestartQueryTimer()
    {
        // Start or reset a pending query
        if (queryTimer == null)
        {
            queryTimer = new Timer { Enabled = true, Interval = 500 };
            queryTimer.Tick += queryTimer_Tick;
        }
        else
        {
            queryTimer.Stop();
            queryTimer.Start();
        }
    }

    void queryTimer_Tick(object sender, EventArgs e)
    {
        // Stop the timer so it doesn't fire again unless rescheduled
        RevokeQueryTimer();

        // Perform the query
        Trace.WriteLine(String.Format("Performing query on text \"{0}\"", SearchField.Text));
    }
}

这篇关于C#延迟SQL查询,而在一个文本框输入用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆