what is the standard way of using sql in C# to increment a counter
column in a shared control table? ( example: the next order number )
I would like to sql select a row in a table and lock it. Then run code
that assigns the new value to a column in the row. Then sql update the
column in the row, releasing the lock.
thanks,
SqlDataReader rdr = null;
SqlCommand cmd = null;
// get the current RowId
cmd = new SqlCommand();
cmd.CommandText =
"SELECT a.NextOrderNumber " +
"from SystemControlTable a " ;
rdr = cmd.ExecuteReader();
bool rc = rdr.Read();
int orderNumber = (Int32) rdr.GetSqlInt32(0);
int nextOrderNumber = CalcNextOrderNumber( orderNumber ) ;
// update the control record with the incremented rowId.
cmd = new SqlCommand();
cmd.CommandText =
"update @TableName " +
"set NextOrderNumber = @NextOrderNumber " ;
cmd.ExecuteNonQuery();