Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing an SQLCommand without specifying a Transaction

We have some lists of data being fetched in our application via a SqlCommand performing a SELECT query on a SQL Server database. We do not explicitly setup a transaction on the SqlCommand, instead just passing it a SqlConnection and running it. Is it the case that when no transaction is specified that SQL Server will initiate and use a default transaction with the default IsolationLevel of ReadCommitted?

like image 207
Graham Avatar asked Jun 19 '09 09:06

Graham


People also ask

How does SqlCommand work in C#?

SqlCommand in C# allow the user to query and send the commands to the database. SQL command is specified by the SQL connection object. Two methods are used, ExecuteReader method for results of query and ExecuteNonQuery for insert, Update, and delete commands. It is the method that is best for the different commands.

What is the SqlCommand object used for?

A SqlCommand object allows you to query and send commands to a database. It has methods that are specialized for different commands. The ExecuteReader method returns a SqlDataReader object for viewing the results of a select query. For insert, update, and delete SQL commands, you use the ExecuteNonQuery method.

Which function should be called for executing select query or stored procedure with select query using the SqlCommand object?

SqlCommand.CommandText Property (System.Data.SqlClient) Gets or sets the Transact-SQL statement, table name or stored procedure to execute at the data source.


1 Answers

SQL creates a transaction implicitly for your statements, and this transaction is committed when the statement completes. The isolation level of this transaction will be the current isolation level, which defaults to READ COMMITTED. Certain statements overwrite the current isolation level and enforce READ COMMITTED (eg. RECEIVE).

If your SqlCommand executes a batch (more statements) then each statement that access tables will create its own transaction.

The default autocommit of transactions is controlled by changing the SET IMPLICIT_TRANSACTION ON.

See Controlling Transactions for more details.

like image 154
Remus Rusanu Avatar answered Oct 06 '22 20:10

Remus Rusanu