Dataset: how to fix the primary key value returned by SELECT SCOPE_IDENTITY ()?

I am using a dataset. I have an Adapter table called PackageTableAdapter that contains the InsertPackage method .

INSERT INTO [dbo].[Packages] ([UserID], [Name]) VALUES (@UserID, @Name)

//Return the PackageID value for the newly created record...
SELECT SCOPE_IDENTITY()

This table adapter is used by a mid-level class with the code below:

private PackagesTableAdapter _packagesTableAdapter = null;
protected PackagesTableAdapter Adapter
{
    get
    {
        if (_packagesTableAdapter == null)
            _packagesTableAdapter = new PackagesTableAdapter();

        return _packagesTableAdapter;
    }
}    

public bool AddPackage(string PackageName)
{
    // Create a new PackageRow instance
    Album.PackagesDataTable packages = new AlbumCongo.PackagesDataTable();
    Album.PackagesRow package = packages.NewPackagesRow();

    // Add the new package
    package.Name = PackageName;
    packages.AddPackagesRow(package);
    int rowsAffected = Adapter.Update(packages);       

    // Return true if precisely one row was inserted,
    // otherwise false
    return rowsAffected == 1;        
}

How can I capture the primary key of a newly created package (the one that should have been returned by the SELECT SCOPE_IDENTITY () statement )?

EDIT

Instead of returning a bool value, I would like to return a custom object that contains both a bool value and an int representing the identifier of the newly created row.

Thanks for the help.

+3
source share
2

, Scaler. , insert Method properties.

-, , Adapter method :

Int32 ID = Convert.ToInt32(Adapter.Insert(parameters...);
+2

PackageID DataRow Adapter.Update.

+1

All Articles