gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:47:34 26 4
gpt4 key购买 nike

我收到错误消息“无法隐式转换类型‘int?’到'int'。在返回行的我的 OrdersPerHour 上存在显式转换(你是否缺少强制转换?)”。我不确定为什么,因为我的 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;
}

最佳答案

那么您要将 OrdersPerHour 转换为 int?

OrdersPerHour = (int?)dbcommand.ExecuteScalar();

然而你的方法签名是int:

static int OrdersPerHour(string User)

两者必须匹配。


还有一个快速建议 -> 在查询中使用参数,例如:

string query = "SELECT COUNT(ControlNumber) FROM Log WHERE DateChanged > ? AND User = ? AND Log.EndStatus in ('Needs Review', 'Check Search', 'Vision Delivery', 'CA Review', '1TSI To Be Delivered')";
OleDbCommand dbcommand = new OleDbCommand(query, conn);
dbcommand.Parameters.Add(curTime.AddHours(-1));
dbcommand.Parameters.Add(User);

关于c# - 无法将类型 'int?' 隐式转换为 'int' 。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16798269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com