【问题标题】:Creating a search function in c#, windows form application在c#中创建搜索功能,windows窗体应用程序
【发布时间】:2018-12-06 18:00:40
【问题描述】:

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

【问题讨论】:

  • 您应该能够从按钮单击事件的文本框中提取值。
  • 到目前为止你尝试了什么?
  • @Lewis 找到我的答案,我已将链接放在整个解决方案所在的位置

标签: c# visual-studio dataset


【解决方案1】:

请从此链接参考您的答案(连同数据库查询)和解释可用

Reference 1

Reference 2

【讨论】:

  • 感谢您的帮助,我的导师说我自己做的事情太复杂了,只需要创建一个简单的程序,所以我使用数据集并将文本框链接到该数据集,以便所有信息显示而不是搜索并显示某些信息。还是谢谢!!
【解决方案2】:

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

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

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

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

【讨论】:

  • 感谢您的帮助,我的导师说我自己做的事情太复杂了,只需要创建一个简单的程序,所以我使用数据集并将文本框链接到该数据集,以便所有信息显示而不是搜索并显示某些信息。还是谢谢!!
【解决方案3】:

在我的例子中,我使用了一个dataGridView作为mysql数据的DataSet,下面是搜索框的示例代码。

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;

                    
                }
            }

欢迎您。

【讨论】:

    【解决方案4】:

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

    获取输入文本

    Textboxname.text;
    

    查询

    SELECT * ON table WHERE tagoridorwhatever = textboxname.text
    

    查询部分可能有点不同,因为我在手机上想出了这个问题

    【讨论】:

    • 感谢您的帮助,我的导师说我自己做的事情太复杂了,只需要创建一个简单的程序,所以我使用数据集并将文本框链接到该数据集,以便所有信息显示而不是搜索并显示某些信息。还是谢谢!!
    猜你喜欢
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 2023-02-10
    • 2018-04-28
    • 1970-01-01
    相关资源
    最近更新 更多