C# 使用Visual Studio从MS Access db通过C中的特定字段搜索数据

C# 使用Visual Studio从MS Access db通过C中的特定字段搜索数据,c#,database,visual-studio,ms-access-2016,icsharpcode,C#,Database,Visual Studio,Ms Access 2016,Icsharpcode,我正在从事一个项目,我必须在该项目中执行一项任务,即我必须通过特定文本或关键字打印/查找MS Access Database 2016文件中的数据。我将尝试所有方法,但无法解决我的问题。因此,在尝试了所有方法后,我决定将我的问题发布在此处,以获得解决问题的帮助。 我附上了我的代码,你可以看到,但代码没有执行任何事情,我得到了一个错误,而试图搜索任何错误 mscorlib.dll中发生类型为“System.FormatException”的第一次意外异常 其他信息:输入字符串的格式不正确。 如果存

我正在从事一个项目,我必须在该项目中执行一项任务,即我必须通过特定文本或关键字打印/查找MS Access Database 2016文件中的数据。我将尝试所有方法,但无法解决我的问题。因此,在尝试了所有方法后,我决定将我的问题发布在此处,以获得解决问题的帮助。 我附上了我的代码,你可以看到,但代码没有执行任何事情,我得到了一个错误,而试图搜索任何错误

mscorlib.dll中发生类型为“System.FormatException”的第一次意外异常 其他信息:输入字符串的格式不正确。 如果存在此异常的处理程序,则程序可以安全地继续

这就是我在执行任务时面临的错误

namespace Vechile_Registration_System
{
public partial class Verification : Form
{
    public Verification()
    {
        InitializeComponent();
    }

    private void Verification_Load(object sender, EventArgs e)
    {

    }

    private void btnsearch_Click(object sender, EventArgs e)
    {
        searchDataBase();
    }
    private void searchDataBase()
    {
        string strsearch = txtsearch.Text.Trim().ToString();

        StringBuilder sb = new StringBuilder();
        vehicleBindingSource.Filter = string.Format("[Registration No] LIKE '%{0}%'", strsearch);

        string strFilter = sb.ToString();
        vehicleBindingSource.Filter = strFilter;

        if (vehicleBindingSource.Count != 0)
        {
            dataGridView1.DataSource = vehicleBindingSource;
        }
        else
        {
            MessageBox.Show("No Records Found. \n The Vehcile may not register or you have enter wrong Registration Number.");
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form1 MMenu = new Form1();
        MMenu.ShowDialog();
    }
}
}请仔细阅读:

您删除了一个非常重要的行,因此,没有数据加载到数据源中。 我们正在对Verification.cs文件进行第一次更改。按如下方式更改验证负载:

    private void Verification_Load(object sender, EventArgs e)
    {
        vehicleTableAdapter.Fill(vehicleDataSet.Vehicle);
        // If you want the grid view to show no data at the beginning
        // Uncomment the following line
        // vehicleBindingSource.Filter = "1 = 0";
    }
您设法删除了searchbutton\u Click事件处理程序。 请严格按照建议执行以下步骤:

在解决方案资源管理器中,双击Verification.cs。这将在设计模式下打开表单。 在表单上,右键单击搜索按钮并从菜单中选择属性。 在顶部的属性窗口中,有一些图标。查找具有thunderbolt lightning形状的事件图标。点击这个。 现在,在最顶端,有一个点击事件。 请非常小心地键入并输入btnsearch\u单击 就这些

希望这有帮助

如果有,请标记为答案

****原始答案****

这应该能解决你的问题

请严格使用此代码

private void searchDataBase()
{
    string strsearch = txtsearch.Text.Trim().ToString();

    StringBuilder sb = new StringBuilder();
    vehicleBindingSource.Filter = string.Format("[Registration No] LIKE '%{0}%'", strsearch);

    if (vehicleBindingSource.Count != 0)
    {
        dataGridView1.DataSource = vehicleBindingSource;
    }
    else
    {
        MessageBox.Show("No Records Found. \n The Vehcile may not register or you have enter wrong Registration Number.");
    }
}

评论不用于扩展讨论;此对话已结束。此代码无法解决我的问题,现在当我单击“搜索”按钮时,代码不会生成任何输出。您可以将您的解决方案上载到何处以便我进行修复吗?刚刚在聊天中向您发送了一条消息,我在其中附加了一个链接,您可以从中下载解决方案。如果你修复了它,请通过聊天告诉我。我正在更新我的答案。请检查它的顶部。你犯了错误,我会告诉你如何改正。请标记为答案,如果它修复了它,它现在正在为我工作。搜索并可以查找和显示记录