D
Daniel Bello Urizarri
Hello:
Im developing some application that is connected to a Sql Server and I need
to read a value and increment it in a multiuser envirnment. I'm trying to
lock the table at the moment of reading the data, but setting a breakpoint
after the select command is executed, the table is readable from another
connection. The blocking starts after the update query and until the
transaction is commited.
Is there some Ado.NET standard way to do what im trying? (I perform some
test with BeginTransaction too. Same results)
What Im i doing wrong?
Thanks
Daniel
Mi code is:
SqlTransaction transaction = null;
try
{
transaction = connection.BeginTransaction ();
SqlCommand cmd = transaction.Connection.CreateCommand ();
cmd.Transaction= transaction;
cmd.CommandText= "Select * From IndexControl TABLOCKX";
//Efectuando la lectura con el suspuesto bloqueo
DataTable table= new DataTable ();
SqlDataAdapter adapter = new SqlDataAdapter ( cmd );
adapter.Fill ( table );
int index = Convert.ToInt32 ( table.Rows [0]["Index"] );
// .. .. .. .. .. otras operaciones..... .. . ... .
index++;
cmd.CommandText= "update IndexControl set Index=" + index;
cmd.ExecuteNonQuery ();
transaction.Commit ();
}
catch
{
if ( transaction != null ) transaction.RollBack ();
}
Im developing some application that is connected to a Sql Server and I need
to read a value and increment it in a multiuser envirnment. I'm trying to
lock the table at the moment of reading the data, but setting a breakpoint
after the select command is executed, the table is readable from another
connection. The blocking starts after the update query and until the
transaction is commited.
Is there some Ado.NET standard way to do what im trying? (I perform some
test with BeginTransaction too. Same results)
What Im i doing wrong?
Thanks
Daniel
Mi code is:
SqlTransaction transaction = null;
try
{
transaction = connection.BeginTransaction ();
SqlCommand cmd = transaction.Connection.CreateCommand ();
cmd.Transaction= transaction;
cmd.CommandText= "Select * From IndexControl TABLOCKX";
//Efectuando la lectura con el suspuesto bloqueo
DataTable table= new DataTable ();
SqlDataAdapter adapter = new SqlDataAdapter ( cmd );
adapter.Fill ( table );
int index = Convert.ToInt32 ( table.Rows [0]["Index"] );
// .. .. .. .. .. otras operaciones..... .. . ... .
index++;
cmd.CommandText= "update IndexControl set Index=" + index;
cmd.ExecuteNonQuery ();
transaction.Commit ();
}
catch
{
if ( transaction != null ) transaction.RollBack ();
}