嗨,我正在制定一个员工评估计划,我需要获取每个字段的每个员工的平均值,以实现这一点,员工每次被评估时都会被插入一个新行,以免覆盖前一个人的评估那个员工我有我的代码并且没有抛出错误但没有将平均值写入数据库
public void AvOut()
{
try
{
string request = tbEmployee.Text;
//Opens a connection to the database
OleDbConnection conn = new OleDbConnection(ConnString);
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT [Admin].[OutPuts] FROM [Admin] WHERE EmployeeName ='" + request + "';";
OleDbDataReader reader = cmd.ExecuteReader();
int total = 0;
int count = 0;
while (reader.Read())
{
total += (int)reader["OutPuts"];
count++;
}
int avg = total / count;
OleDbCommand cmds = conn.CreateCommand();
cmds.CommandText = "SELECT MAX(EmployeeID) AS [id_no] FROM [Admin];";
OleDbDataReader dbReader = cmds.ExecuteReader();
dbReader.Read();
int id = Convert.ToInt32(dbReader["id_no"]);
OleDbCommand tot = conn.CreateCommand();
tot.CommandText = (@"UPDATE [Admin] SET AVGOutput ='" + avg + "' WHERE EmployeeID = " + id);
int affected = tot.ExecuteNonQuery();
conn.Close();
}
catch (Exception e)
{
MessageBox.Show("The exception is " + e.ToString());
}
}