0

嗨,我正在制定一个员工评估计划,我需要获取每个字段的每个员工的平均值,以实现这一点,员工每次被评估时都会被插入一个新行,以免覆盖前一个人的评估那个员工我有我的代码并且没有抛出错误但没有将平均值写入数据库

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());
    }
}
4

1 回答 1

1

您可以尝试删除 avg 周围的单引号。所以你的查询应该是:

tot.CommandText = (@"UPDATE [Admin] SET AVGOutput =" + avg + " WHERE EmployeeID = " + id);

还要设置一个断点,看看你是否在 avg 中得到任何东西并检查 id

于 2012-05-10T14:15:35.670 回答