473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

using sql to lock, select, then update

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();

Mar 14 '07 #1
2 3781
On Mar 14, 11:29 am, "Steve Richter" <StephenRich...@gmail.comwrote:
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();
ALTER PROCEDURE [dbo].[apGetNextSequenceNumber]
@SequenceKey int,
@SequenceNumber int output
AS
BEGIN
SET NOCOUNT ON

UPDATE SequenceNumber
SET @SequenceNumber = NextSequenceNumber = NextSequenceNumber + 1
WHERE SequenceKey = @SequenceKey

END

(In this case, I have several possible sequences which are independant
of each other). Execute this statement to get the next number and
then use that number in your order. Make sure its done within a
transaction.

HTH
Andy

Mar 14 '07 #2
On 14 Mar 2007 08:29:40 -0700, Steve Richter wrote:
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();
What I would suggest is that you do the incrementing and assignment within
*the same* stored procedure. You can then wrap the procedure in a
transaction and set the appropriate isolation level (I forget exactly
which!)
--
Bits.Bytes
http://bytes.thinkersroom.com
Mar 14 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Andrew Baker | last post by:
What is the best way to lock an individual row in the following scenerio: --TODO - LOCK THIS ROW -- Return the next id SELECT next_id INTO next_id_out FROM owner.my_id_table WHERE app_id =...
0
by: geoff | last post by:
I'm running the same application on 4.0.1-alpha-win and 4.0.15-win. Under 4.0.15 I run the same test client as I run against 4.0.1-alpha-win. The behavior is different. In particular, 4.0.15...
4
by: Richard Holliingsworth | last post by:
Hello: I have an Access 2K form I built from a SQL Server 7.0 view. I want to lock certain fields in the database from users so they can see them on the views and forms, but NOT be able to edit...
5
by: xixi | last post by:
hi, i am using db2 udb v8.1 on windows, i create a index on a unique value column on a table to try to create row lock, here is what i do , sql = select value from table where id=1 for update,...
4
by: xeqister | last post by:
Greetings, We are connecting to a DB2 database residing on an AIX machine via JDBC. In order to lock some row of records, we use the "select . . . for update . . . " Java statement: In a...
5
by: Uwe C. Schroeder | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, maybe my mind is stuck, but here's something strange. This is the classic "counter" thing, where you can't / won't use sequences....
25
by: Mark Harrison | last post by:
I would like to try and build a queuing mechanism on top of Postgresql. Imagine an application where a large number of processes generate images and queue up thumbnail requests. A smaller number...
2
by: adri4n | last post by:
as wat ive mentioned in the title.. im would like to know whether the a particular record/table is being locked in my program. some of the methods which i would like to develop are as below: ...
4
by: misterutterbag | last post by:
Hi. Given the following: ---------------- String lockSubscriptionQuery = "SELECT subscriptionId FROM subscription WHERE subscriptionId = ? FOR UPDATE"; pStmt =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.