简体   繁体   English

从winforms文本框中立即从sql数据库中获取数据

[英]immediate fetching of data from sql database from winforms textbox

I am using Winforms for my application & SQL Server as database. 我正在为我的应用程序和SQL Server使用Winforms作为数据库。

I want that as soon as any text is typed in a textbox , immediate results are fetched/searched from the SQL SERVER database tables for that supplied text. 我希望在文本框中键入任何文本后,立即从SQL SERVER数据库表中获取/搜索该提供的文本的即时结果。

For this , i have given the following query: 为此,我给出了以下查询:

    public partial class Form1 : Form
{
    SqlConnection conn = new SqlConnection();
    public Form1()
    {
        conn.ConnectionString = "Trusted_Connection=true";
        conn.Open();
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

        DataTable dt = null;

        SqlCommand cmd = new SqlCommand ("SELECT * FROM items WHERE item_name LIKE'" +    textBox1.Text + "%'", conn);
        SqlDataReader reader = cmd.ExecuteReader();

        dt = new DataTable();

        dt.Load(reader);

        dataGridView1.DataSource = dt;

    }
}

But , as this fetches data every time from the database, so it takes more time, but i want a faster way. 但是,由于每次都会从数据库中获取数据,因此需要更多时间,但是我想要一种更快的方法。 so shall i use DATASETS for this purpose, as datasets are used for disconnected environment. 所以我应该为此目的使用DATASETS,因为数据集用于断开连接的环境。

OR 要么

I shall first fetch the whole ITEM table from the database on to a GridView , & display it when the Form is opened. 我将首先从数据库中获取整个ITEM表到GridView,并在打开Form时显示它。

now, when text is entered in the textbox , then it would not fetch data from the sql database, but would search in the GridView, so would this be faster? 现在,当在textbox中输入文本时,它将不会从sql数据库中获取数据,而是会在GridView中进行搜索,这样会更快吗?

which way would be efficient? 哪种方法有效?

The item table has 3.4 million records. 项目表有340万条记录。

How big is your items table? 您的物品表有多大?

If it's not big, it'll do to just store it in a dataset. 如果不大,则可以将其存储在数据集中。 Use the same textbox but search in the dataset. 使用相同的文本框,但在数据集中搜索。

If it's big, I would suggest using a timer. 如果很大,我建议您使用计时器。 On each textchange, restart the timer of maybe 0.5 seconds. 每次进行文本更改时,请重新启动计时器(可能需要0.5秒)。 when the timer has elapsed, then only query the database. 当计时器过去时,则仅查询数据库。 This prevents multiple queries while the user is typing. 这样可以防止在用户键入时进行多个查询。

Alternatively, if you could read the whole table and assign it to the AutoCompleteCustomSource : 或者,如果您可以读取整个表并将其分配给AutoCompleteCustomSource

textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
foreach(DataRow row in dt.Rows)
    textBox1.AutoCompleteCustomSource.Add(row["item_name"] as string);

Yes. 是。 Using a dataset and searching on it would be much faster. 使用数据集并对其进行搜索会更快。 Since you are using WinForms, memory footprint is probably also not an issue unless you you are fetching a huge number of rows from the database. 由于使用的是WinForms,除非您从数据库中获取大量的行,否则内存占用也可能不是问题。

Also, you should probably not search on every text change, but wait for a small amount of time say 2 seconds during which there are no changes to the textbox and then fetch. 同样,您可能不应该搜索每一个文本更改,而应等待一小段时间(例如2秒钟),在此期间文本框没有更改,然后进行提取。 Otherwise you would be fetching for any new character entered in the textbox (i think). 否则,您将获取在文本框中输入的任何新字符(我认为)。

Better approach will be using DataSet / DataTable. 更好的方法是使用DataSet / DataTable。 Read all the data from the Table on the form load and store it in the Form. 从表格读取表格加载时的所有数据,并将其存储在表格中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM