您好我正在制作员工评估计划,我需要获得每个员工每个领域的平均值才能实现这一点,每次评估员工时,他们都会被插入一个新行,以免覆盖上一个人对该员工的评价我有我的代码,并没有抛出错误,但没有将平均值写入数据库
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());
}
}
答案 0 :(得分:1)
您可以尝试删除avg周围的单引号。所以你的查询应该是:
tot.CommandText = (@"UPDATE [Admin] SET AVGOutput =" + avg + " WHERE EmployeeID = " + id);
同时提出一个断点,看看你是否得到平均值的任何东西,并检查id