DataAccesLayer并非现在所有代码路径都返回一个值为什么? [英] DataAccesLayer Not all code paths return a value even now Why?

查看:64
本文介绍了DataAccesLayer并非现在所有代码路径都返回一个值为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataAccesslayer中的代码:

Code in DataAccesslayer:

public DataSet FindRecord(int RollNumber)
{
    try
    {
        using (SqlConnection con = new SqlConnection(cs))
        {
            DataSet ds = new DataSet();
            //string cmd = "Select * from tblstudent where RollNumber=@RollNumber";
            SqlCommand cmd = new SqlCommand("spfindtblstudent", con);
            cmd.Parameters.AddWithValue("@RollNumber", RollNumber);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            
            da.Fill(ds);
            con.Close();
    
            return ds;
        }
    
       
    }
    catch(Exception ex)
    {
        string str = ex.Message;
    }
}



即使我写的返回类型的方法仍然是我得到相同的原因?

有人帮我


even i written the Return type of method still i'am getting the same Why?
Some one help me

推荐答案

你得到这个错误,因为如果有例外,你的代码中的return语句就不会到达。



你有一些选择:



1.删除try-catch子句。

你没有做现在无论如何都有用。

让上层处理异常。



2.重新抛出错误

You get this error because the return statement in your code will not be reached if there is an exception.

You have some choices:

1. Remove the try-catch clause.
You are not doing anything useful there right now anyway.
Let the upper layer handle the exception.

2. Re-throw the error
catch(Exception ex)
{
    throw ex;
}



这与选项1基本相同。



3.返回null in catch子句。


This is basically the same as option 1.

3. Return null in the catch clause.

catch(Exception ex)
{
    Debug.WriteLine(ex.ToString());
    return null;
}


这篇关于DataAccesLayer并非现在所有代码路径都返回一个值为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆