【发布时间】:2017-03-13 10:48:22
【问题描述】:
我为我的考勤监控系统创建了一个过滤器搜索,它按员工姓名搜索,但不搜索其他字段,我想我会使用 LIKE 关键字但我不知道该怎么做。这是我的代码
private void textBox1_TextChanged(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "SELECT * FROM tblEmployee WHERE [Firstname] like @1";
command.parameters.AddWithValue("@1",textBox1.Text);
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Update();
dataGridView1.Refresh();
connection.Close();
}
【问题讨论】:
-
这段代码有什么问题?你有什么错误吗?
-
您要过滤DataTable还是数据库?
-
command.parameters.AddWithValue("@1",string.Format("{0}%", textBox1.Text));这就是你想要的吗?
-
如果您的应用程序变得更复杂,我建议在查询的控件、事件和参数中使用更具描述性的名称。维护起来会容易得多。
-
尝试根据您的要求构建/连接您的查询与 if else
标签: c# select search datagridview datatable