使用 Visual Studio 从 MS Access 数据库通过 C# 中的特定字段搜索数据
Searching data through a specific Field in C# from MS Access db using Visual Studio
我正在处理一个项目,我必须在该项目中执行一项任务,我必须通过特定的文本或关键字 print/find 来自 MS Access 数据库 2016 文件的数据我会尝试一切,但无法解决我的问题,所以在尝试了所有方法后我决定 post 我的问题在这里得到一些帮助来解决它。
我附上了我的代码,你可以看到,但该代码没有执行任何操作,我在尝试搜索任何内容时遇到错误,错误是
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
If there is a handler for this exception, the program may be safely continued.
这是我在执行任务时遇到的错误。
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 文件进行第一次更改。像这样更改 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";
}
- 您以某种方式设法删除了 searchbutton_Click 事件处理程序。
请完全按照他们的建议执行这些步骤:
- 在解决方案资源管理器中双击 Verification.cs。这应该会在设计模式下打开表单。
- 在表单上,右键单击“搜索”按钮,然后从菜单中单击 select 属性。
- 在顶部的属性 window 中,有一些图标。找到雷电(闪电)形状的 "Events" 图标。点击这个。
- 现在,在最顶部,有 "Click" 事件。
- 输入时要非常小心
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.");
}
}
我正在处理一个项目,我必须在该项目中执行一项任务,我必须通过特定的文本或关键字 print/find 来自 MS Access 数据库 2016 文件的数据我会尝试一切,但无法解决我的问题,所以在尝试了所有方法后我决定 post 我的问题在这里得到一些帮助来解决它。 我附上了我的代码,你可以看到,但该代码没有执行任何操作,我在尝试搜索任何内容时遇到错误,错误是
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format. If there is a handler for this exception, the program may be safely continued.
这是我在执行任务时遇到的错误。
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 文件进行第一次更改。像这样更改 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";
}
- 您以某种方式设法删除了 searchbutton_Click 事件处理程序。
请完全按照他们的建议执行这些步骤:
- 在解决方案资源管理器中双击 Verification.cs。这应该会在设计模式下打开表单。
- 在表单上,右键单击“搜索”按钮,然后从菜单中单击 select 属性。
- 在顶部的属性 window 中,有一些图标。找到雷电(闪电)形状的 "Events" 图标。点击这个。
- 现在,在最顶部,有 "Click" 事件。
- 输入时要非常小心
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.");
}
}