Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Scope_identity()
Message
De
10/10/2003 01:20:36
 
 
À
09/10/2003 22:40:41
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Divers
Thread ID:
00837243
Message ID:
00837265
Vues:
34
Mike,

Keith and Steve have given you good answers, but if I may add one more suggestion and that is to use an OUTPUT parameter in your Stored Proc instead of returning a key value. In fact, we use the same SP for both inserts and updates, defining an INSERT when the primarykey is NULL or 0, and an UPDATE otherwise:
CREATE PROCEDURE MyTable_Put
    @MyPK int = NULL OUTPUT,
    @parm2 char(2) = NULL,
    @parm3 char(3) = NULL
AS
    IF (@MyPK = 0 OR @MyPK IS NULL)
    BEGIN
        INSERT INTO MyTable (MyColumn2, MyColumn3)
            SELECT @parm2, @parm3

        SELECT @MyPK = SCOPE_IDENTITY()
    END
    ELSE
        UPDATE MyTable
        SET MyColumn2 = ISNULL(@parm2, MyColumn2),
            MyColumn3 = ISNULL(@parm3, MyColumn3)
        WHERE MyPKColumn = @MyPK
Then, in your DataAccess method, you just insert the row into the Parent table first, get the newly added row's PK by using the Parameters collection, stick that into the FK in the child table and then insert the child row. And be sure to wrap the whole thing up in a transaction.

~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform