如何创建搜索框

本文关键字:搜索 创建 何创建 | 更新日期: 2023-09-27 18:14:23

现在我已经花了几个小时试图解决问题,并在谷歌周围和阅读论坛,但我还没有找到我的答案!

我需要做一些"商业应用"作为我学校的一个项目,在那里我需要保持对公司客户的概述。我使用Microsoft Visual c# 2010 Express和Access 2010,并使用OleDb在c#中编写。

我的问题是:

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

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

这里是代码,在。fill (dataset, "Food");中得到一个错误。InvalidOperationException未处理。填充:SelectCommand。连接尚未初始化。只是测试一个简单的访问数据库与一个表命名为"食品"与FoodID, FoodName和价格里面。
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];
        }
     }

如何创建搜索框

你能张贴代码,你有,不工作?

在搜索按钮的点击事件中,你可以调用一个函数来填充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];
}

我的电脑上没有Visual Studio,所以可能会有语法错误,但这应该让你开始。

我在实习的时候也做过类似的事情。我解决这个问题的方法是为我正在搜索的每种类型的数据创建一个表单,然后在附加到数据网格视图的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;
}

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