【问题标题】:How to create a search box?如何创建搜索框?
【发布时间】:2011-12-27 10:15:51
【问题描述】:

现在我花了几个小时试图解决一些问题并在谷歌上搜索和阅读论坛,但我还没有找到答案!

我需要制作某种“业务应用程序”作为我学校的一个项目,在那里我需要对公司的客户进行全面了解。 我正在使用 Microsoft Visual C# 2010 Express 和 Access 2010,并使用 OleDb 用 C# 编写。

我的问题是:

如何为应用程序创建搜索框/表单以搜索访问数据库 (.accdb) 中的信息。 我想使用一个文本框,从我的数据库中写一些东西,例如公司名称,然后按下搜索按钮。现在它应该写入与公司名称相关的所有信息,这些信息在 DataGrid 的数据库中找到。

也许这是一个太大的项目,所以如果有人有一个不太复杂的搜索功能做类似的事情,我也会很感激。

这是代码,.Fill(dataset, "Food"); 中有错误。 InvalidOperationException 未处理。填充:SelectCommand.Connection 尚未初始化。 只需测试一个简单的访问数据库,其中包含一个名为“Food”的表,其中包含 FoodID、FoodName 和 Price。

private void button1_Click(object sender, RoutedEventArgs e)
    {
        GetCustomers(textBox1.Text);
    }
        // Load Data from the DataSet into the DataGridView
        private void GetCustomers(string searchTerm)
        {
            DataSet dataset = new DataSet();
            using (OleDbConnection connection = 
            new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Niclas\Desktop\Skole\Programmering\Database\Food.accdb;Persist Security Info=False;"))
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                adapter.SelectCommand = new OleDbCommand(
                "select * from Food where FoodID like ('" + searchTerm + "', connection");
                adapter.Fill(dataset, "Food");
            }

        // Get the table from the data set and bind the data to the grid
        this.dataGrid1 = new System.Windows.Controls.DataGrid();

        dataGrid1.DataContext = dataset.Tables[0];

        }
     }

【问题讨论】:

  • 您的问题到底是什么?如何创建搜索框?在表单上放置一个文本框。然后,在搜索按钮下,通过您使用的 OleDB 驱动程序使用 SQL 查询来选择带有 WHERE 子句作为文本框条件的数据。
  • 你好..这正是我的问题,但我是新手,所以我会感谢一些编码方面的帮助:) 谢谢。

标签: c# .net database search ms-access-2010


【解决方案1】:

您可以发布您拥有的不起作用的代码吗?

基本上在搜索按钮的单击事件中,您将调用一个函数来填充 DataGridView,例如:

GetCustomers(textBox1.Text);


// Load Data from the DataSet into the DataGridView
private void GetCustomers(string searchTerm)
{
DataSet dataset = new DataSet();
using (OleDbConnection connection = 
    new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;"))
  {
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    adapter.SelectCommand = new OleDbCommand(
        "select * from customer where name like %" + searchTerm + "%", connection);
    adapter.Fill(dataset, "Customers");
  }

    // Get the table from the data set and bind the data to the grid
    DataGridView.DataSource = dataset.Tables[0];
}

我在这台 PC 上没有 Visual Studio,因此可能存在语法错误,但这应该可以帮助您入门。

【讨论】:

  • 我不会做"...%" + searchTerm + "%" ...这对以一种或另一种形式的SQL注入是开放的。如果可能的话,使用参数化查询。我意识到这是一个学校项目,但教授良好做法并避免此类问题是值得做的:-)
  • @ChrisJ - 我同意并且我应该给出专业建议 - 使用 SQL Server、MySQL 等。但是 MS Access 没有存储过程,那么您将如何进行参数化查询?我将把 SQL 注入攻击留给 OP 来研究......
  • 参数化查询不需要是存储过程。动态查询也可以通过在查询中使用占位符参数。 Microsoft 有一篇关于 Access 和 ADO 的文章——我希望 ADO.NET 会非常相似...support.microsoft.com/kb/200190
  • 非常感谢!像这样的东西我一直在寻找。如果我 c/p 并且当然编辑源和从/从哪里,这会起作用吗?
  • 使用以下代码启动应用程序。但是当我在我的文本框中输入“1”(在我的访问数据库中查找 nr 1 foodid)时,我收到一个错误:(在“FoodID like %1%”中有语法错误的东西。*用代码更新了帖子。
【解决方案2】:

我在实习时做过这样的事情。我解决这个问题的方法是为我正在搜索的每种类型的数据创建一个表单,然后在附加到数据网格视图的 BindingSource 上运行一个过滤器。我添加的另一个巧妙之处是此过滤器在击键时运行,因此它会在您键入时自动运行。

我有一个类似这样的方法(这是从 VB 转换而来的):

private void Search()
{
    string[] strSplitString = null;
    string strSearchString = "";
    strSplitString = Strings.Split(txtSearch.Text.Trim);

    // Check to see if there are any strings
    if (strSplitString.Count == 1) {
        // Construct the search string
        strSearchString = "FirstName Like '%" + strSplitString[0] + "%' Or MiddleName Like '%" + strSplitString[0] + "%' Or LastName Like '%" + strSplitString[0] + "%'";

    } else if (strSplitString.Count > 1) {
        // Construct the string
        strSearchString = "(FirstName Like '%" + strSplitString[0] + "%' Or MiddleName Like '%" + strSplitString[0] + "%' Or LastName Like '%" + strSplitString[0] + "%')";

        // For each word add it to the search string
        for (intWord = 1; intWord <= (strSplitString.Count - 1); intWord++) {
            strSearchString += " And (FirstName Like '%" + strSplitString[intWord] + "%' Or MiddleName Like '%" + strSplitString[intWord] + "%' Or LastName Like '%" + strSplitString[intWord] + "%')";
        }
    }

    // Filter the binding source
    BindingSource.Filter = strSearchString;
}

那里可能有更好的方法,但是这个项目也是针对一个相当大的访问数据库并且似乎工作得很好

【讨论】:

  • 我不会这样做"...%" + strSplitString[0] + "%" ...这对以一种或另一种形式的SQL注入开放。如果可能的话,使用参数化查询。我意识到这是一个学校项目,但教授良好做法并避免此类问题是值得的 :-) [我也认为我正在遭受似曾相识]
  • @ChrisJ 非常正确。但是,如果我回想起创建此应用程序时的情况,那么尝试减轻 Access 中的 SQL 注入是一项相当费力的任务。我个人的偏好是不使用 Access,而是使用 SQL Server Compact Edition 之类的东西,并在顶部使用 EF 来防止这种攻击
  • 你不需要 EF ... 请参阅 support.microsoft.com/kb/200190 了解如何使用 ADO 进行查询;将其转换为 ADO.NET 应该不会花费太多时间。
  • @ChrisJ 我忘了这一切。这可能是实现预期效果的更简单方法。 EF 在某些地方可能会很笨重
  • 嗨.. 如果我有一个文本框并在单击按钮下复制/粘贴它,这是否有效?
猜你喜欢
  • 2023-03-05
  • 2012-05-14
  • 1970-01-01
  • 2017-07-16
  • 2021-04-07
  • 1970-01-01
  • 2010-09-25
  • 1970-01-01
  • 2019-03-16
相关资源
最近更新 更多