G
Guest
Hi,
I'm using ADO.Net SqlCommand objects to do Database reads/updates in an
application. I'd like to use optimistic concurrency, so each of my tables
has a Column of type TimeStamp in it.
Right now I am NOT using stored procedures. I am updating the CommandText
property of the SqlCommand object in code, and using command.executeReader or
command.executeNonQuery to read or update data.
I understand that the TimeStamp column will be automatically updated by SQL
Server (2005) when I Insert or Update a row. What is the best method for
getting the new timestamp value back into my application after a database
write?
I think I have a couple of options...
1) Just run another SELECT statement after the row update (with
command.executeScalar) to get back the TimeStamp. I can do this, but it just
seems cumbersome, and it has to be slow running two separate commands.
2) I've seen lots of examples with Stored Procedures and output parameters
- but I'm not using sproc's. Can I define one value for CommandText with
both the Update and the Select in it, have them both execute on
ExecuteNonQuery and get the new Timestamp back in an output parameter? If
this is the case, what does the SQL syntax look like for this? I've tried
using some example SQL that I've seen used with sproc's ...
cm.CommandText =
"Update A Set Name = @Name, Data = @Data WHERE Id = @Id and TS = @TS
Select @NewTS = TS FROM A WHERE Id = @Id"
@Id, @Name, @Data, and @TS are all regular input parms,
@NewTS is set up as an output parm before running the command like this...
cm.parameters.Add("@NewTS", SqlDBType.Timestamp)
cm.Parameters("@NewTS").Direction = ParameterDirection.Output
I use executeNonQuery to run the command, but nothing comes back in my
output parm.
mTS = cm.Parameters("@NewTS").Value ' Value comes back as Nothing
What's the best way to do this, and if it's an output parm, please include
an example of the SQL Syntax.
Thanks.
BBM
I'm using ADO.Net SqlCommand objects to do Database reads/updates in an
application. I'd like to use optimistic concurrency, so each of my tables
has a Column of type TimeStamp in it.
Right now I am NOT using stored procedures. I am updating the CommandText
property of the SqlCommand object in code, and using command.executeReader or
command.executeNonQuery to read or update data.
I understand that the TimeStamp column will be automatically updated by SQL
Server (2005) when I Insert or Update a row. What is the best method for
getting the new timestamp value back into my application after a database
write?
I think I have a couple of options...
1) Just run another SELECT statement after the row update (with
command.executeScalar) to get back the TimeStamp. I can do this, but it just
seems cumbersome, and it has to be slow running two separate commands.
2) I've seen lots of examples with Stored Procedures and output parameters
- but I'm not using sproc's. Can I define one value for CommandText with
both the Update and the Select in it, have them both execute on
ExecuteNonQuery and get the new Timestamp back in an output parameter? If
this is the case, what does the SQL syntax look like for this? I've tried
using some example SQL that I've seen used with sproc's ...
cm.CommandText =
"Update A Set Name = @Name, Data = @Data WHERE Id = @Id and TS = @TS
Select @NewTS = TS FROM A WHERE Id = @Id"
@Id, @Name, @Data, and @TS are all regular input parms,
@NewTS is set up as an output parm before running the command like this...
cm.parameters.Add("@NewTS", SqlDBType.Timestamp)
cm.Parameters("@NewTS").Direction = ParameterDirection.Output
I use executeNonQuery to run the command, but nothing comes back in my
output parm.
mTS = cm.Parameters("@NewTS").Value ' Value comes back as Nothing
What's the best way to do this, and if it's an output parm, please include
an example of the SQL Syntax.
Thanks.
BBM