使用 c# 获取多行的平均值(以 ms access 2010 为单位)
本文关键字:ms access 2010 为单位 平均值 获取 使用 | 更新日期: 2023-09-27 18:33:50
嗨,我
正在制定一个员工评估程序,我需要获取每个字段每个员工的平均值才能完成此操作,每次评估员工时都会被放入新行,以免覆盖前一个人对该员工的评估我有我的代码并且没有抛出错误,但没有将平均值写入数据库
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());
}
}
您可以尝试删除 avg 两边的单引号。所以你的查询应该是:
tot.CommandText = (@"UPDATE [Admin] SET AVGOutput =" + avg + " WHERE EmployeeID = " + id);
还要放一个断点,看看你是否在avg中得到任何东西,并检查id