Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to ensure 2 SQL Server Operations atomic?

Author  Topic 

lucky7456969
Starting Member

6 Posts

Posted - 2015-03-11 : 07:58:38
[code]
SqlConnection conn = new SqlConnection();
try
{
conn.ConnectionString = "Data Source=localhost\\sqlexpress;Initial Catalog=RCS_2_02;Integrated Security = SSPI";
SqlCommand command = new SqlCommand(
@"UPDATE IssueLog SET Actual_Finish_Date = @Actual_Finish_Date
WHERE ItemNo = @ItemNo", conn);

// todo add a record to approval for finished job approval

command.Parameters.AddWithValue("@Actual_Finish_Date", Finish_Date.ToShortDateString());
command.Parameters.AddWithValue("@ItemNo", ItemNo);
conn.Open();

int res = command.ExecuteNonQuery();
conn.Close();
return 0;
}
catch (Exception e)
{
conn.Close();
return -1;
}
[/code]

In the todo line, when the approval is not inserted, I want
the update of the IssueLog to rollback, how can I achieve that?
Thanks
Jack

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-11 : 08:50:34
Wrap the SQL commands in a transaction. see here for explanation and examples:
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction%28v=vs.110%29.aspx
Go to Top of Page

lucky7456969
Starting Member

6 Posts

Posted - 2015-03-11 : 10:18:32
Thanks
Go to Top of Page
   

- Advertisement -