Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何创建搜索框?_C#_.net_Database_Search_Ms Access 2010 - Fatal编程技术网

C# 如何创建搜索框?

C# 如何创建搜索框?,c#,.net,database,search,ms-access-2010,C#,.net,Database,Search,Ms Access 2010,现在我已经花了几个小时试图解决一些问题,在谷歌上搜索和阅读论坛,但我还没有找到我的答案 我需要在我的学校做一些“商业应用程序”作为一个项目,在那里我需要对一家公司的客户进行概述。 我正在使用Microsoft Visual C#2010 Express和Access 2010,并使用OleDb以C#编写 我的问题是: 如何为应用程序创建搜索框/表单以搜索access数据库(.accdb)中的信息。 我想使用一个文本框,在这里我可以从数据库中写入一些东西,例如公司的名称,并按下搜索按钮。现在,它应

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

我需要在我的学校做一些“商业应用程序”作为一个项目,在那里我需要对一家公司的客户进行概述。 我正在使用Microsoft Visual C#2010 Express和Access 2010,并使用OleDb以C#编写

我的问题是:

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

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

这是代码,在.Fill(数据集,“Food”);中出错;。 未处理InvalidOperationException。Fill:SelectCommand。连接尚未初始化。 只需测试一个简单的access数据库,其中有一个名为“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];

        }
     }

你能把你的代码贴出来吗

基本上,在搜索按钮的单击事件中,您将调用一个函数来填充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()
{
字符串[]strSplitString=null;
字符串strSearchString=“”;
strSplitString=Strings.Split(txtSearch.Text.Trim);
//检查是否有任何字符串
if(strSplitString.Count==1){
//构造搜索字符串
strSearchString=“类似于“%”+strSplitString[0]+“%”的名字或类似于“%”+strSplitString[0]+“%”的中间名或类似于“%”+strSplitString[0]+“%”的姓氏;
}else if(strSplitString.Count>1){
//构造字符串
strSearchString=“(名字类似“%”+strSplitString[0]+“%”或中间名类似“%”+strSplitString[0]+“%”或姓氏类似“%”+strSplitString[0]+“%”);
//对于每个单词,将其添加到搜索字符串中

对于(intWord=1;intWord您到底有什么问题?如何创建搜索框?在表单上放置一个文本框。然后,在“搜索”按钮下,通过OleDB驱动程序使用SQL查询,您正在使用WHERE子句作为您的文本框条件来选择数据。您好..这正是我的问题,但我是一个新手,因此我希望在编码方面得到一些帮助:)谢谢。我不会做
“…%”+searchTerm+“%”
…这是一种开放的SQL注入形式。如果可能的话,使用参数化查询。我意识到这是一个学校项目,但教授良好实践和避免此类问题是值得的:-)@ChrisJ-我同意,我应该给出专业建议-使用SQL Server、MySQL等。但MS Access没有Order proc的,那么你将如何进行参数化查询呢?我将把SQL注入黑客留给OP来研究……参数化查询不需要是存储过程。动态查询也可以,通过在查询中使用占位符参数。Microsoft有一篇关于Access和ADO的文章——我希望它与ADO.NET非常相似……谢谢很多!像这样的东西我一直在寻找。如果我c/p,当然,编辑源代码和从哪里来,这会起作用吗?让应用程序运行以下代码。但当我在文本框中键入“1”(在access数据库中查找nr 1 foodid)时,我得到一个错误:(在“foodid like%1%”中有一个语法错误的东西).*用代码更新了帖子。我不会做
“…%”+strSplitString[0]+“%”
…这是一种开放的SQL注入形式。如果可能的话,请使用参数化查询。我意识到这是一个学校项目,但教好的实践并避免此类问题是值得做的:-)[我还认为我遇到了似曾相识]@ChrisJ非常正确。但如果我记得我创建此应用程序时,试图减轻Access中的SQL注入是一项相当艰巨的任务。我个人的偏好是不使用Access,而是使用SQL Server Compact Edition之类的工具,并在顶部使用EF来防止这种攻击。您不需要EF…有关如何使用ADO进行查询,请参阅;将其转换为ADO.NET应该不会花费太多时间。@ChrisJ我完全忘记了这一点。这可能是实现所需效果的一种更简单的方法。EF在位置上可能相当笨重。如果我有一个文本框并在“单击”按钮下复制/粘贴它,这是否有效?
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;
}