SELECT @@IDENTITY and ExecuteScalar conversion

Posts   
 
    
mayvelous avatar
mayvelous
User
Posts: 5
Joined: 06-Feb-2007
# Posted on: 20-Feb-2007 06:11:47   

I'm very new to LLBLgen thing and am trying to convert the old insert sqlcommand code to llblgen method. I'm having a problem understanding how/which llblgen code to use for SELECT @@Indentity and ExecuteScalar call.

Part of the original code is something like this -

SqlCommand command = new SqlCommand("INSERT INTO tblABC (field1, field2, field3, field4l) VALUES (@Value1, @Value2, @Value3, @Value4)", connection);
command.Parameters.AddWithValue("@Value1", value1);
command.Parameters.AddWithValue("@Value2", value2);
command.Parameters.AddWithValue("@Value3", value3);
command.Parameters.AddWithValue("@Value4", value4);
command.ExecuteNonQuery();
command.CommandText = "SELECT @@IDENTITY";
_fieldID = command.ExecuteScalar().ToString();
connection.Close();

I've change the first part to the followings:

tblABCEntity myEntity = new tblABCEntity();
myEntity.field1 = value1;
myEntity.field2 = value2;
myEntity.field3 = value3;
myEntity.field4 = value4;

using (DataAccessAdapter adapter = new DataAccessAdapter())
{
        adapter.SaveEntity(myEntity);
}

And now I'm stuck at the second part conversion. I search through in the forum and found this post(http://llblgen.com/TinyForum/Messages.aspx?ThreadID=6801&HighLight=1) but not sure how exactly to utalize it in my code. I'm just completely lost here. rage Any help plz?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Feb-2007 06:29:17   

I assume you want to fetch back the PK (Identity Column) of the newly inserted entity. am I right?

If so you don't need to call anything, the Select @@ Identity is automatically called when you save the entity, and the PK field should be filled for you.

And if you want to re-fetch any other field after the save, just pass true to the SaveEntity method. adapter.SaveEntity(myEntity, true);

mayvelous avatar
mayvelous
User
Posts: 5
Joined: 06-Feb-2007
# Posted on: 21-Feb-2007 02:22:11   

O...K. I'm trying to return the ID of the newly inserted row. You are saying, I can just add true to SaveEntity and refetch that recently inserted new row ID rite?

So I should be able to do like this and re-fetch the inserted ID:

adapter.SaveEntity(myEntity, true);
_fieldID = myEntity.FieldID;

and if I don't have true, that would fetch the old ID?

adapter.SaveEntity(myEntity);
_fieldID = myEntity.FieldID;

I think I'm confusing myself. Thanks for replying. I'll test it out and see how it turn out.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Feb-2007 07:17:30   

Using true (for refetch) or not, anyway the NEW PK is returned.

But in case you wanted to fetch other fields you should use the re-fetch option (true). Say you have some default values set by the database for some columns, if you want to fetch those fields, you should use the Save overload which accepts a boolean for the refetch.