【问题标题】:I get the error 'not all code paths return a value' in a try catch block我在 try catch 块中收到错误“并非所有代码路径都返回值”
【发布时间】:2014-03-31 00:13:46
【问题描述】:

请看下面的代码,它是 MVC,我正在尝试创建一个 IEnumerable 视图。我得到的错误是'不是所有代码路径都返回一个值'我该如何纠正错误?

public class CustomerSummary
{
    public string ContactName { get; set; }     // Customer table
    public string City { get; set; }            // Customer table
    public string PostalCode { get; set; }      // Order table
    public string ShipName { get; set; }        // Order table
    public string ProductName { get; set; }     // Product table
    public bool Discontinued { get; set; }      // product table

}

控制器

public class CustomerSummaryController : Controller
{
    //
    // GET: /CustomerSummary/
    private CustomerSummaries _customerSummaries = new CustomerSummaries();

    public ViewResult Index()
    {
        IEnumerable<CustomerSummary> summaries = _customerSummaries.GetAll();
        return View(summaries);
    }

}

数据层

public IEnumerable<CustomerSummaries> GetAll(/* to do put connection string here */)
    {
        try
        {
            SqlCommand cmd = new SqlCommand("GetAll", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            SqlDataReader sdr;
            conn.Open();
            sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                if (sdr.IsDBNull(sdr.GetOrdinal("ContactName")) != true)
                {
                    sdr["ContactName"].ToString();
                }
            }


        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            conn.Close();
        }
    } 

【问题讨论】:

  • 您的数据层没有返回任何内容。这就是您收到该错误的原因。您需要返回一个IEnumerable&lt;CustomerSummaries&gt; 对象。
  • 另外,这个:sdr["ContactName"].ToString(); 是空操作。
  • 为什么要捕获(异常){抛出; } ???这样做有什么好处?

标签: c# asp.net-mvc


【解决方案1】:

我在这里做了很多假设,但我认为这就是你想要的:

public IEnumerable<CustomerSummary> GetAll(SqlConnection conn)
{
    var result = new List<CustomerSummary>();

    try
    {
        SqlCommand cmd = new SqlCommand("GetAll", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        SqlDataReader sdr;
        conn.Open();
        sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            var cs = new CustomerSummary();

            if (sdr.IsDBNull(sdr.GetOrdinal("ContactName")) != true)
            {
                cs.ContactName = sdr["ContactName"].ToString();
            }

            // repeat the above if-block to add more info if needed...

            // add the CustomerSummary to the result
            result.Add(cs);
        }
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        conn.Close();
    }

    return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2014-04-16
    • 2015-02-07
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多