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

前端 未结 3 929
梦谈多话
梦谈多话 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:46
    <asp:SqlDataSource runat="server":
        SelectCommand="SELECT ... "
        UpdateCommand="UPDATE a; UPDATE b;"
        DeleteCommand="DELETE FROM a; DELETE FROM b;" />
    

    Doesn't it work?

    0 讨论(0)
  • 2020-12-21 06:50
    CREATE PROC UpdateUser
    (
        @ID int
        ,@Pass varchar(15)
        ,@Email varchar(75)
       ) AS
    BEGIN
    
    Update
       tbl_user_login
    Set
       Pass = @Pass
    Where
       ID = @ID
    
    Update
      tbl_user_profile
    Set
      Email1 = @Email
    Where
      ID = @ID
    
    END
    

    You can write multiple sql commands into a single sproc as I have above. Expand updates as needed. Deletes work the same way...

    0 讨论(0)
  • 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 <tablename> WHERE <condition>

    0 讨论(0)
提交回复
热议问题