我正在尝试创建一个应用程序,允许某人通过 ID 搜索确切结果,或者如果您不知道 ID,您可以搜索他们姓名的一部分以返回匹配的帐户。
例如 Bob Jones 是 ID A01,您可以搜索 A01 以返回 Bob Jones 或搜索 B 以返回 Bob Jones、Bradley Smith 等。
由于某种原因,我对 ID 部分进行了很好的编码,当它实际上是相同的代码时,第二部分不起作用。
private void btnSaveEmployees_Click(object sender, EventArgs e)
{
{
//First search for the entered text to see if it is an ID
DataBase db = new DataBase(); //Construct a new database.
db.OpenConnection(); //Open our Database connection
MySqlCommand mysqlCmd = new MySqlCommand("SELECT * FROM `employees` WHERE `employee_ID` = @employeeID LIMIT 1", db.connection);
mysqlCmd.Parameters.AddWithValue("@employeeID", txtEmployeeID.Text);
MySqlDataReader reader = mysqlCmd.ExecuteReader(); //Construct a reader
if (reader.Read())
{
//Employee ID has been found so lets update the update form.
Form employee = new UpdateEmployee(reader["firstName"].ToString(), reader["lastName"].ToString(), reader["contactNumber"].ToString(), reader["employee_access_level"].ToString());
employee.Show();
}
else
{
db.CloseConnection();
db.OpenConnection();
MySqlDataReader Reader2;
MySqlCommand mysqlCmd2 = new MySqlCommand("SELECT firstName, LastName FROM `employees` WHERE `firstName` LIKE '"+txtEmployeeID.Text+"';", db.connection);
//mysqlCmd2.Parameters.AddWithValue("@textbox", txtEmployeeID.Text);
Reader2 = mysqlCmd2.ExecuteReader(); //Construct a reader
while (Reader2.Read()==false)
{
listboxFindEmployees.Visible = true;
string thisrow = "";
for (int i = 0; i < Reader2.FieldCount; i++)
thisrow += Reader2.GetValue(i).ToString() + ",";
listboxFindEmployees.Items.Add(thisrow);
}
}
}
}