在C#Windows窗体应用程序中创建搜索功能

时间:2018-06-27 18:47:10

标签: c# visual-studio dataset

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

4 个答案:

答案 0 :(得分:0)

请从此链接获取参考(您的答案(以及数据库查询),并提供说明)

Reference 1

Reference 2

答案 1 :(得分:0)

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

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

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

4)将结果列表输入到您的输出文本框中。

答案 2 :(得分:0)

就我而言,我使用了 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;

                    
                }
            }

欢迎您。

答案 3 :(得分:-2)

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

获取输入文字

Textboxname.text;

查询

SELECT * ON table WHERE tagoridorwhatever = textboxname.text

查询部分可能会有所不同,因为我是用手机将其弄乱了

相关问题