How to write update query to update two tables with SQL Data Source?

前端 未结 3 951
梦谈多话
梦谈多话 2020-12-21 06:25

Is it possible to update two tables using SQL Data Source and ASP.NET Grid View? I have the following SQL Query for the Select statement.

SELECT 
   tbl_user         


        
3条回答
  •  自闭症患者
    2020-12-21 06:54

    Yes, it's possible. Here is how to do it in a safe way:

    CREATE PROCEDURE UpdateUser (@ID int, @Pass varchar(15), @Email varchar(75)) AS
    
    BEGIN TRANSACTION
    
        UPDATE tbl_user_login SET Pass = @Pass WHERE ID = @ID
    
        IF @@ERROR <> 0
        BEGIN
            ROLLBACK
            RETURN
        END
    
        UPDATE tbl_user_profile SET Email1 = @Email WHERE ID = @ID
    
        IF @@ERROR <> 0
        BEGIN
            ROLLBACK
            RETURN
        END
    
    COMMIT
    

    In this way you don't need to be worried in case there is some error raised by any of the updates. Which means: If any of them will fail, the whole operation will be canceled and you will not have inconsistent data in your DB.

    More about the use of Transactions here: http://www.4guysfromrolla.com/webtech/080305-1.shtml

    The basic DELETE statement is: DELETE FROM WHERE

提交回复
热议问题