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



我正在处理一个项目,我必须在该项目中执行一项任务,即我必须通过特定的文本或关键字从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();
}
}

}

请仔细阅读:

  1. 您删除了一个非常重要的行,因此,没有数据加载到您的数据源中

我们正在对Verification.cs文件进行第一次更改。将Verification_Load更改如下:

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";
}
  1. 您已经设法删除了searchbutton_Click事件处理程序

请按照建议应用这些步骤:

  • 在解决方案资源管理器中双击Verification.cs。这将在设计模式下打开表单
  • 在表单上,右键单击"搜索"按钮,然后从菜单中选择"属性">
  • 在顶部的"属性"窗口中,有一些图标。找到具有雷电形状的"事件"图标。单击此项
  • 现在,在最上面,有"点击"事件
  • 键入并输入btnsearch_Click时要非常小心

仅此而已。

希望这能有所帮助。

如果是,请标记为答案



***原始答案***

这应该能解决你的问题。

请准确使用此代码。

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.");
}
}

最新更新