怎么给Try,catch块? [英] How to give Try ,catch blocks ?
本文介绍了怎么给Try,catch块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public int? FarmInfraDetailsUpdate(FarmRecordPL FormrecordPL)
{
SqlParameter[] arParams = new SqlParameter[7];
arParams[0] = new SqlParameter("@shedid", typeof(int));
arParams[0].Value = FormrecordPL.shedid;
arParams[1] = new SqlParameter("@shedno", typeof(int));
arParams[1].Value = FormrecordPL.shedno;
arParams[2] = new SqlParameter("@length", typeof(int));
arParams[2].Value = FormrecordPL.length;
arParams[3] = new SqlParameter("@width", typeof(int));
arParams[3].Value = FormrecordPL.width;
arParams[4] = new SqlParameter("@totalarea", typeof(int));
arParams[4].Value = FormrecordPL.totalarea;
arParams[5] = new SqlParameter("@capacity", typeof(int));
arParams[5].Value = FormrecordPL.capacity;
arParams[6] = new SqlParameter("@username", typeof(string));
arParams[6].Value = FormrecordPL.capacity;
return Convert.ToInt16(SqlHelper.ExecuteScalar(connection.ConnectionString, CommandType.StoredProcedure, "k_UpdateFarmRecordInfraDetail", arParams));
}
推荐答案
发生错误时需要返回一些内容,以便与事实相符这个必须返回一个int。
You need to return something when an error happens, to be consistent with the fact that this must return an int.
public int? FarmInfraDetailsUpdate(FarmRecordPL FormrecordPL)
{
try
{
//Your code...
return Convert.ToInt16(SqlHelper.ExecuteScalar(connection.ConnectionString, CommandType.StoredProcedure, "k_UpdateFarmRecordInfraDetail", arParams));
}
catch
{
//Now you need to return something else
return 0; //Just a suggestion!
}
}
我假设你在这里使用了 nullable int 返回类型,这样你就可以在更新记录时检测到错误。
那么,为什么不在catch块中返回 null,所以:当你的方法返回时,你可以测试返回的值是否为null,并处理记录的事实没有更新?
I assume you used a nullable int return Type here so you could detect an error in updating the record.
So, why not return a null in the catch-block, so: when your method returns, you can test if the returned value is null, and handle the fact that the record was not updated ?
这篇关于怎么给Try,catch块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文