无法隐式转换类型'int?'到"int". [英] Cannot implicitly convert type 'int?' to 'int'.
本文介绍了无法隐式转换类型'int?'到"int".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到错误消息无法隐式转换类型'int?'到'int'.在返回行上我的OrdersPerHour上存在显式转换(您是否需要强制转换?).我不知道为什么,因为我的C#技能没有那么先进.任何帮助将不胜感激.
I'm getting the error "Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)" on my OrdersPerHour at the return line. I'm not sure why because my C# skills are not that advanced. Any help would be appreciated.
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?
Well you're casting OrdersPerHour
to an int?
OrdersPerHour = (int?)dbcommand.ExecuteScalar();
但是您的方法签名是 int
:
static int OrdersPerHour(string User)
这两个必须匹配.
还有一个快速建议-> 在查询中使用参数,例如:
Also a quick suggestion -> Use parameters in your query, something like:
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);
这篇关于无法隐式转换类型'int?'到"int".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文