日期:2009-10-03 浏览次数:20457 次
[SQL SERVER7.0 or SQL SERVER2000] BEGIN TRAN INSERT INTO PRODUCT(PRODUCTID, PRODUCTNAME) VALUES("0001", "KEYBOARD") UPDATE PRODUCT SET PRICE=12 WHERE PRODUCTID= "0002" IF (@ERROR>0) ROLLBACK ELSE COMMIT |
try { //数据库操作命令 } catch (Exception e) { ?//如果有异常发生,这部分语句将被执行 } finally { ?//无论是否有异常发生,这部分语句都会得到执行 } |
[Store Procedure] CREATE PROCEDURE PRODUCT_SAVE( AS DECLARE (@USERID CHAR(5), @LOCATION VARCHAR(50), @RETURNS INT OUTPUT ) BEGIN TRAN UPDATE ADDRESS SET LOCATION=@LOCATION WHERE USERID=@USERID IF (@@ERROR>0) BEGIN @RETURNS=-1 /* Fail to Update */ ROLLBACK END ELSE @RETURNS=0 /* Succeed to update */ COMMIT RETURN @RETURNS [Web Application in C#] int values; DBClass dbc=new DBClass(); // 使用new命令生成一个数据库类 values=dbc.updatedb("0001", "23 Rain Street"); //And call its function member to update record if (values==0) Lable_message.text= "Update successfully"; else Lable_message.text= "Sorry, can not update this record, please contact your DBA." |
[Web Applicaion in C#] SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"); myConnection.Open(); SqlTransaction myTrans = myConnection.BeginTransaction(); //使用New新生成一个事务 SqlCommand myCommand = new SqlCommand(); myCommand.Transaction = myTrans; try { myCommand.CommandText = "Update Address set location='23 rain street' where userid='0001'"; myCommand.ExecuteNonQuery(); myTrans.Commit(); Console.WriteLine("Record is udated."); } catch(Exception e) { myTrans.Rollback(); Console.WriteLine(e.ToString()); Console.WriteLine("Sorry, Record can not be updated."); } finally { myConnection.Close(); } |