When should I use the using Statement? [duplicate]

If a class implements IDisposable then it will create some unmanaged resources which need to be ‘disposed’ of when you are finished using them. So you would do something like: SqlConnection awesomeConn = new SqlConnection(connection); // Do some stuff awesomeConn.Dispose(); To avoid forgetting to dispose of the resourses (in this case close the database connection), … Read more

Do using statements and await keywords play nicely in c#

Yes, that should be fine. In the first case, you’re really saying: Asynchronously wait until we can get the response Use it and dispose of it immediately In the second case, you’re saying: Asynchronously wait until we can get the response Asynchronously wait until we’ve logged the response Dispose of the response A using statement … Read more

The C# using statement, SQL, and SqlConnection

It’s possible to do so in C# (I also see that code is exactly shown in MSDN http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx). However, if you need to be defensive and for example log potential exceptions which would help troubleshooting in a production environment, you can take this approach: private static void CreateCommand(string queryString, string connectionString) { using (SqlConnection connection … Read more

When is “using” block used for in C#? How to use “using” block in C#?

Because it also implements IDisposable. The purpose of Using statement is that when control will reach end of using it will dispose that object of using block and free up memory. its purpose is not only for auto connection close, basically it will dispose connection object and obviously connection also closed due to it. Its … Read more

Why use a using statement with a SqlTransaction?

A using statement should be used every time you create an instance of a class that implements IDisposable within the scope of a block. It ensures that the Dispose() method will be called on that instance, whether or not an exception is thrown. In particular, your code only catches managed exceptions, then destroys the stack … Read more

Does Java have a using statement?

Java 7 introduced Automatic Resource Block Management which brings this feature to the Java platform. Prior versions of Java didn’t have anything resembling using. As an example, you can use any variable implementing java.lang.AutoCloseable in the following way: try(ClassImplementingAutoCloseable obj = new ClassImplementingAutoCloseable()) { … } Java’s java.io.Closeable interface, implemented by streams, automagically extends AutoCloseable, … Read more

Will a using statement rollback a database transaction if an error occurs?

Dispose method for transaction class performs a rollback while Oracle’s class doesn’t. So from transaction’s perspective it’s implementation dependent. The using statement for the connection object on the other hand would either close the connection to the database or return the connection to the pool after resetting it. In either case, the outstanding transactions should … Read more