【发布时间】:2018-09-24 03:36:59
【问题描述】:
当我使用 if 和 else if 语句编写返回数据表的方法时出现错误。什么是正确的格式。提前致谢 “并非所有代码路径都返回值”
[HttpGet]
public DataTable getcareerdata (int tclass, int efkey)
{
if (tclass ==7)
{
return obj.GetData(string.Format(@"select Effect_Class_Key , From_Value , To_Value , Effect_Class_Value, Scientific_Degree, case When Scientific_Degree is null then 'Not Related'
when Scientific_Degree = 0 then 'Not Related' when Scientific_Degree = -1 then 'Not Related' else latin_desc end as Spec from hr_effect_classes Left Outer Join general_cod
on hr_effect_classes.Scientific_Degree = general_cod .sub_cod and main_cod = 17001 where type_class in (7) and status_class = 1 and effect_key = {0}
Order By Scientific_Degree , From_Value ", efkey));
}
else if (tclass == 8)
{
return obj.GetData(string.Format(@"select Effect_Class_Key , From_Value , To_Value , Effect_Class_Value, Scientific_Degree case When Scientific_Degree is null then 'Not Related'
when Scientific_Degree = 0 then 'Not Related' when Scientific_Degree = -1 then 'Not Related' else L_Desc end as Spec from hr_effect_classes Left Outer Join HR_Specialty
on hr_effect_classes.Scientific_Degree = HR_Specialty.Spec_Key where type_class in (8) and status_class = 1 and effect_key = {0}
Order By Scientific_Degree , From_Value ",efkey));
}
}
【问题讨论】:
-
在 tclass 不 = 7 或 8 的情况下,没有返回数据表。你如何处理这取决于你的需要。
-
你需要一个包罗万象的
else吗? -
您可以在末尾添加
return null以避免编译错误。 -
在类的顶部(在 if 之外)声明一个 DataTable,然后在 if 语句中填充该 Datatable,并在底部的 if 之外执行 RETURN。
-
我认为您可以找到数千个此错误的重复项。下次发帖前先搜索一下吧
标签: c#