public IEnumerable<Order> GetOrders()
{
    var orders = new List<Order>();
    using (var con = new SqlConnection("some connection string"))
    {
        using (var cmd = new SqlCommand("select * from orders", con))
        {
            using (var rs = cmd.ExecuteReader())
            {
                while (rs.Read())
                {
                    // ...
                }
            }
        }
    }
    return orders;
}
出现using嵌套,影响代码的美观
对其改进:
public IEnumerable<Order> GetOrders()
{
    var orders = new List<Order>();

    using (var con = new SqlConnection("some connection string"))
    using (var cmd = new SqlCommand("select * from orders", con))
    using (var rs = cmd.ExecuteReader())
    {
        while (rs.Read())
        {
            // ...
        }
    }
    return orders;
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
  • 2022-12-23
  • 2022-02-22
  • 2021-12-05
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
  • 2022-12-23
  • 2022-02-28
  • 2021-10-26
  • 2022-12-23
相关资源
相似解决方案