using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand mySqlCommand = new SqlCommand("Select EmpID, Name from Employee WHERE EmpID = @EmpID OR Name = @Name ", myDatabaseConnection))
{
mySqlCommand.Parameters.AddWithValue("@EmpID", textBox1.text);
mySqlCommand.Parameters.AddWithValue("@Name", textBox1.text);
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(mySqlCommand);
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
}
样本数据库
EmpID Name
4001 Johnny Bravo
4002 Bruce Smith
4003 Vince Walker
当我在 textBox1 上输入 4001 这是结果
EmpID Name
4001 Johnny Bravo
当我在 textBox1 上输入 Bruce Smith 时,这就是结果
EmpID Name
4002 Bruce Smith
我需要的是显示记录是通过 EmpID 还是 Name 找到的。例如,我输入 4003 文本框或标签等将类似于 "The record is found by EmpID" 。当我输入 Vince Walker 时,它会像 "The record is found by Name" 。