无法隐式将类型'int?'转换为'int'。

15 浏览
0 Comments

无法隐式将类型'int?'转换为'int'。

在我的OrdersPerHour的返回行上,我遇到了错误“无法将类型'int?'隐式转换为'int'。存在一个显式转换(是否缺少一个强制转换?)”。我不确定为什么会出现这个错误,因为我的C#技能还不够高级。如果有任何帮助,我将非常感激。

static int OrdersPerHour(string User)
{
    int? OrdersPerHour;
    OleDbConnection conn = new OleDbConnection(strAccessConn);
    DateTime curTime = DateTime.Now;        
    try
    {
        string query = "SELECT COUNT(ControlNumber) FROM Log WHERE DateChanged > #" + curTime.AddHours(-1) + "# AND User = '" + User + "' AND Log.EndStatus in ('Needs Review', 'Check Search', 'Vision Delivery', 'CA Review', '1TSI To Be Delivered');";
        OleDbCommand dbcommand = new OleDbCommand(query, conn);
        dbcommand.Connection.Open();
        dbcommand.CommandType = CommandType.Text;
        OrdersPerHour = (int?)dbcommand.ExecuteScalar();
        Console.WriteLine("Orders per hour for " + User + " is " + OrdersPerHour);            
    }
    catch (OleDbException ex)
    {
    }
    finally
    {
        conn.Close();
    }
    return OrdersPerHour;
}

0