繁体   English   中英

在c#中创建搜索功能,windows窗体应用程序

[英]Creating a search function in c#, windows form application

我正在创建一个程序作为大学作业的一部分,并且必须有一个连接到我的程序的数据库。 该程序在 c# 中,并在带有 Visual Studio 的 Windows 窗体应用程序中创建。 我需要一个允许输入的文本框,然后有一个按钮来搜索与之匹配的任何值,但我不知道如何读取输入的内容、搜索数据库并将它们返回到文本框中。 我已经连接了数据库,所有的表单都设计并用按钮连接在一起,但是这一部分真的让我感到困惑。 任何帮助将不胜感激。PS 我是 C# 的新手,还没有完全理解它。

请从此链接参考您的答案(以及数据库查询)和说明

参考文献 1

参考文献2

1)将数据库中的所有文本放入某种集合(例如列表)中。

2) 通过访问文本框的 Text 属性从文本框中获取文本。 如果需要,可以进行一些修改,例如删除大写、处理关键字等。

3) 编写一个类似于 collection.Where(t => t.Contains(searchString)).ToList() 的 linq 查询。 或者,您可以循环遍历集合。

4) 将结果列表提供给您的输出文本框。

在我的情况下,我被用于一个DataGridView DataSet MySQL数据的和下面是用于搜索框的示例代码。

private void tfSearch_TextChanged(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(tfSearch.Text) == false)
            {
                dataGridView1.Rows.Clear();
                for(int i = 0; i < GlobalState.Items.Tables[0].Rows.Count; i++)
                {
                    string id = GlobalState.Items.Tables[0].Rows[i].ItemArray.GetValue(0).ToString();
                    string name = GlobalState.Items.Tables[0].Rows[i].ItemArray.GetValue(1).ToString();
                    string price = GlobalState.Items.Tables[0].Rows[i].ItemArray.GetValue(2).ToString();
                    string stock = GlobalState.Items.Tables[0].Rows[i].ItemArray.GetValue(3).ToString();
                    if (name.StartsWith(tfSearch.Text))
                    {
                        int index = dataGridView1.Rows.Add();
                        dataGridView1.Rows[index].Cells[0].Value = id;
                        dataGridView1.Rows[index].Cells[1].Value = name;
                        dataGridView1.Rows[index].Cells[2].Value = price;
                        dataGridView1.Rows[index].Cells[3].Value = stock;
                    }
                }
            }
            else if(tfSearch.Text == "")
            {
                dataGridView1.Rows.Clear();
                for (int i = 0; i < GlobalState.Items.Tables[0].Rows.Count; i++)
                {
                    string id = GlobalState.Items.Tables[0].Rows[i].ItemArray.GetValue(0).ToString();
                    string name = GlobalState.Items.Tables[0].Rows[i].ItemArray.GetValue(1).ToString();
                    string price = GlobalState.Items.Tables[0].Rows[i].ItemArray.GetValue(2).ToString();
                    string stock = GlobalState.Items.Tables[0].Rows[i].ItemArray.GetValue(3).ToString();

                    int index = dataGridView1.Rows.Add();
                    dataGridView1.Rows[index].Cells[0].Value = id;
                    dataGridView1.Rows[index].Cells[1].Value = name;
                    dataGridView1.Rows[index].Cells[2].Value = price;
                    dataGridView1.Rows[index].Cells[3].Value = stock;

                    
                }
            }

别客气。

单击按钮时获取文本框文本,然后使用该搜索词开始查询,这样您就可以获得包含该词的所有内容

获取输入文本

Textboxname.text;

询问

SELECT * ON table WHERE tagoridorwhatever = textboxname.text

查询部分可能有点不同,因为我在手机上想到了这一点

暂无
暂无

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

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