Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CA2000对象“comm”未沿所有异常路径释放_C#_Asp.net - Fatal编程技术网

C# CA2000对象“comm”未沿所有异常路径释放

C# CA2000对象“comm”未沿所有异常路径释放,c#,asp.net,C#,Asp.net,在VS 2010的DAL中,我有很多这样的方法。当我运行新的代码分析选项时,我得到一条消息-警告CA2000对象“comm”未沿所有异常路径释放。在对象“comm”上调用System.IDisposable.Dispose,然后对其进行所有引用,使其超出范围 我知道我可以为SQLCommand使用另一个using语句,但是如果我喜欢像处理Try/Finally块一样使用它的话。我的理解是Finally块最后执行并进行清理。有人看到我的电话有什么问题吗 public List<Produ

在VS 2010的DAL中,我有很多这样的方法。当我运行新的代码分析选项时,我得到一条消息-警告CA2000对象“comm”未沿所有异常路径释放。在对象“comm”上调用System.IDisposable.Dispose,然后对其进行所有引用,使其超出范围

我知道我可以为SQLCommand使用另一个using语句,但是如果我喜欢像处理Try/Finally块一样使用它的话。我的理解是Finally块最后执行并进行清理。有人看到我的电话有什么问题吗

  public List<Product> GetAllProducts()
    {

        List<Product> prodList = new List<Product>();
        using (SqlConnection connection = new SqlConnection(GetConnection()))
        {
            SqlCommand comm = new SqlCommand("GetAllProducts", connection);
            connection.Open();
            comm.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = comm.ExecuteReader();
            try
            {
                while (dr.Read())
                {
                    Product obj = new Product();
                    obj.ProductID = Convert.ToInt32(dr["ProductID"].ToString());
                    obj.Product = dr["Product"].ToString();
                    //etc....                                                                       

                    prodList.Add(obj);
                }
            }
            finally
            {
                comm.Dispose();
                dr.Close();
            }
        }

        return prodList;
    }

}

如果这三条语句中的任何一条抛出异常,comm将不会被释放

        connection.Open();
        comm.CommandType = CommandType.StoredProcedure;
        SqlDataReader dr = comm.ExecuteReader();
public List<Product> GetAllProducts()
{
    List<Product> prodList = new List<Product>();
    using (SqlConnection connection = new SqlConnection(GetConnection()))
    {
        using (SqlCommand comm = new SqlCommand("GetAllProducts", connection))
        {
            connection.Open();
            comm.CommandType = CommandType.StoredProcedure;
            using (SqlDataReader dr = comm.ExecuteReader())
            {
                try
                {
                    while (dr.Read())
                    {
                        Product obj = new Product();
                        obj.ProductID = Convert.ToInt32(dr["ProductID"].ToString());
                        obj.Product = dr["Product"].ToString();
                        //etc....                                                                       

                        prodList.Add(obj);
                    }
                }
            }
        }
    }

    return prodList;
}
您的try块也需要包含这些语句

        List<Measure_Type> MeasureList = new List<Measure_Type>();
        SqlConnection conn = null;            

        SqlDataReader rdr = null;
        SqlCommand cmd = null;
        try
        {
            conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["Conn"];
            conn.Open();
            cmd = new SqlCommand("SELECT Measure_ID, Mea_Name FROM MeasureTable WHERE IsActive=1", conn);

            rdr = cmd.ExecuteReader();
            if (rdr.HasRows == true)
            {
                while (rdr.Read())
                {
                    MeasureTypeList.Add(new Measure_Type { MeasureTypeID = Convert.ToInt32(rdr[0]), MeasureTypeName = rdr[1].ToString() });
                }
            }
        }

        catch (Exception ex)
        {
            ExceptionPolicy.HandleException(ex, "Log");
        }
        finally
        {
            cmd.Dispose();
            // close the reader
            if (rdr != null) { rdr.Close(); }
            // Close the connection
            if (conn != null) { conn.Dispose(); }
        }
        return MeasureTypeList;

创建conn=newsqlconnection;在try块内,然后打开它以解决此错误。

在命令和数据读取器周围放置一个using块,以便始终处理它们

        connection.Open();
        comm.CommandType = CommandType.StoredProcedure;
        SqlDataReader dr = comm.ExecuteReader();
public List<Product> GetAllProducts()
{
    List<Product> prodList = new List<Product>();
    using (SqlConnection connection = new SqlConnection(GetConnection()))
    {
        using (SqlCommand comm = new SqlCommand("GetAllProducts", connection))
        {
            connection.Open();
            comm.CommandType = CommandType.StoredProcedure;
            using (SqlDataReader dr = comm.ExecuteReader())
            {
                try
                {
                    while (dr.Read())
                    {
                        Product obj = new Product();
                        obj.ProductID = Convert.ToInt32(dr["ProductID"].ToString());
                        obj.Product = dr["Product"].ToString();
                        //etc....                                                                       

                        prodList.Add(obj);
                    }
                }
            }
        }
    }

    return prodList;
}

如果我这样做,那么我必须将dr.cose从finally块中删除,因为我收到一条错误消息,当前上下文中不存在名称dr。这有意义吗?我只是想,如果发生错误,我会希望dr.close特别位于finally块中。您需要处理两个不同的对象,这些对象分配在不同的行上。这意味着您要么需要使用两个try/finally块,要么需要将其中一个块初始化为null,并确保在调用dispose之前该块不为null。我想我从未见过两个try/finally块,我觉得这听起来不正确。我刚刚使用SqlCommand comm=new-SqlCommandGetAllProducts实现了连接,代码分析仍然在对象“comm”上显示call System.IDisposable.Dispose,直到对它的所有引用都超出范围。并在对象“connection”上调用System.IDisposable.Dispose,直到对它的所有引用都超出范围。