C# using statement catch error

后端 未结 16 1303
轻奢々
轻奢々 2021-01-30 14:06

I am just looking at the using statement, I have always known what it does but until now not tried using it, I have come up with the below code:

 using (SqlComma         


        
相关标签:
16条回答
  • 2021-01-30 14:48

    FYI, in this specific example, because you're using an ADO.net connection and Command object, be aware that the using statement just executes the Command.Dispose, and the Connection.Dispose() which do not actually close the connection, but simply releases it back into the ADO.net Connection pool to be reused by the next connection.open ... which is good, and the absolutely correct thing to do, bc if you don't, the connection will remain unuseable until the garbage collector releases it back to the pool, which might not be until numerous other connection requests, which would otherwise be forced to create new connections even though there's an unused one waiting to be garbage collected.

    0 讨论(0)
  • 2021-01-30 14:49

    If your code looks like this:

    using (SqlCommand cmd = new SqlCommand(...))
    {
      try
      {
        /* call stored procedure */
      }
      catch (SqlException ex)
      {
        /* handles the exception. does not rethrow the exception */
      }
    }
    

    Then I would refactor it to use try.. catch.. finally instead.

    SqlCommand cmd = new SqlCommand(...)
    try
    {
      /* call stored procedure */
    }
    catch (SqlException ex)
    {
      /* handles the exception and does not ignore it */
    }
    finally
    {
       if (cmd!=null) cmd.Dispose();
    }
    

    In this scenario, I would be handling the exception so I have no choice but to add in that try..catch, I might as well put in the finally clause and save myself another nesting level. Note that I must be doing something in the catch block and not just ignoring the exception.

    0 讨论(0)
  • 2021-01-30 14:53

    If the caller of your function is responsible for dealing with any exceptions the using statement is a nice way of ensuring resources are cleaned up no matter the outcome.

    It allows you to place exception handling code at layer/assembly boundaries and helps prevent other functions becoming too cluttered.

    Of course, it really depends on the types of exceptions thrown by your code. Sometimes you should use try-catch-finally rather than a using statement. My habit is to always start with a using statement for IDisposables (or have classes that contain IDisposables also implement the interface) and add try-catch-finally as needed.

    0 讨论(0)
  • 2021-01-30 14:54

    Minor correction to the example: SqlDataAdapter also needs to be instantiated in a using statement:

    using (SqlConnection con = new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString))
    using (SqlCommand cmd = new SqlCommand(reportDataSource, con))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Year", SqlDbType.Char, 4).Value = year;
        cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value = start;
        cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value = end;
        con.Open();
    
        DataSet dset = new DataSet();
        using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
        {
            adapter.Fill(dset);
        }
        this.gridDataSource.DataSource = dset.Tables[0];
    }
    
    0 讨论(0)
提交回复
热议问题