There are many people who talk about this on the Internet, but this does not seem to work. This is the exception I get:
This SqlTransaction has completed; it is no longer usable.
Here is the code
using (SqlConnection locationConnection = new SqlConnection(connectionString))
{
locationConnection.Open();
SqlTransaction transaction = locationConnection.BeginTransaction();
SqlCommand cmd = new SqlCommand("
Select stuff from table A
Insert stuff into table B
Delete stuff from table A", locationConnection, transaction);
using(SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
}
}
transaction.Commit();
}
Can anyone shed some light on why this is happening? If I move the commit inside the SqlDataReader area, I get an exception that the datareader should close.
EDIT: I answered my question and try not to forget to come, accept it in a couple of days when they let me.
source
share