In my BLL I have a method that adds a new row to a table in the database...
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
public bool AddContact(string firstname, string lastname, string middleinit, bool active, Guid uid, bool newsletter)
{
ContactsDAL.tblContactsDataTable contacts = new ContactsDAL.tblContactsDataTable();
ContactsDAL.tblContactsRow contact = contacts.NewtblContactsRow();
contact.FirstName = firstname;
contact.LastName = lastname;
contact.MiddleName = middleinit;
contact.Active = active;
contact.UserID = uid;
contact.Newletter = newsletter;
contacts.AddtblContactsRow(contact);
int rowsAffected = Adapter.Update(contact);
return rowsAffected == 1;
}
The primary key in this table is a BigInt set as an identity column...How do I capture the value of the primary key that gets created when the new row is added?
Hi,basically the new row that create will be updated with the ID of new inserted record.
However, the following sql piece:
Select SCOPE_IDENTITY()
will do it.
Cheers,
Yani
|||Im familiar with the idea of SCOPE_IDENTITY but am not certain how to use it in a BLL.
How could I modify the code in my BLL method to retreive the SCOPE_IDENTITY?
|||
Basically, the designer does that for you.
For example the generated Insert statements looks like:
CREATEPROCEDURE [dbo].[AdministratorInsert]
(
@.AdministratorIdint,
@.AdministratorNamenvarchar(50),
@.Passwordnvarchar(50),
@.Activebit
)
AS
SETNOCOUNTOFF;
INSERTINTO [Administrator]([AdministratorId], [AdministratorName], [Password], [Active])VALUES(@.AdministratorId, @.AdministratorName, @.Password, @.Active);
SELECT AdministratorId, AdministratorName, Password, ActiveFROM AdministratorWHERE(AdministratorId= @.AdministratorId)
after a record is inserted , it is selected and the DataRow gots updated automatically.
sql
No comments:
Post a Comment