in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

How to return value of autoincrement field after insert

Last post 05-26-2009 4:23 AM by bencmorrow. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 07-14-2006 7:28 AM

    • AlexDP
    • Top 150 Contributor
    • Joined on 06-15-2006
    • Ukraine, Dniepropetrovsk
    • Posts 9

    How to return value of autoincrement field after insert

    Subj.

    For exampe my table has two fileds

    RecordID (autoIncrement integer), Name (string)

    I make such insert

    "insert into people  (Name)

    values (@Name)";

    How can I return value of RecordID, of inserted book?

  • 07-14-2006 7:59 AM In reply to

    Re: How to return value of autoincrement field after insert

    Change your SQL to:

    "insert into people (Name) values(@Name);
     SELECT last_insert_rowid() AS RecordID;"
    ;

    Then call ExecuteScalar() on it instead of ExecuteNonQuery() to get the rowid.

    Also your table definition should be:

    CREATE TABLE People (RecordID INTEGER PRIMARY KEY AUTOINCREMENT, Name VARCHAR);

    Robert

     

  • 07-14-2006 11:11 AM In reply to

    • AlexDP
    • Top 150 Contributor
    • Joined on 06-15-2006
    • Ukraine, Dniepropetrovsk
    • Posts 9

    Re: How to return value of autoincrement field after insert

    Thank you very much.

    I go now to vacations and

    I'll impement this after vacations.

  • 05-26-2009 4:23 AM In reply to

    Re: How to return value of autoincrement field after insert

    Hi, I am trying to return the value of the auto increment field.

     I am using the design time installation of system.data.sqlite in vs2005 with c#.

    I have a table adapter which is inserting into a households table. In the dataset designer I have set the autoincrement seed and step of the households datatable to -1,-1 as recommended by microsoft. I have changed the default insert statement to

    insert into housholds (street,town,postcode) values (@street,@town,@postcode);select last_insert_rowid() as householdno;

     When i create a new household row and then call householdsdataAdapter.Update(householdRow) the row is inserted into the database but the householdno property of the household row is never updated with the newly created id value. I need to retrieve this value to insert child rows into another table.

    I have been able to get it to work by creating a new insert query and calling executescaler but it means i have to supply all the parameters. I want to be able to call householdstableadapter.update(hosueholdrow) and the new row will contain the updated id from the database.

    Do I have to handle the rowupdated event in the same mannor microsoft recommends for the jet 2.0 provider at the link below.

    http://msdn.microsoft.com/en-us/library/ks9f57t0(VS.71).aspx

     

    Thanks

     Ben.

Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems