Getting the Last Insert ID with SQLite.NET in C#

后端 未结 8 1265
执笔经年
执笔经年 2020-11-30 07:20

I have a simple problem with a not so simple solution... I am currently inserting some data into a database like this:

kompenzacijeDataSet.KompenzacijeRow kom         


        
相关标签:
8条回答
  • 2020-11-30 07:55

    last_insert_rowid() is part of the solution. It returns a row number, not the actual ID.

    cmd = CNN.CreateCommand();
    cmd.CommandText = "SELECT last_insert_rowid()";
    object i = cmd.ExecuteScalar();
    
    cmd.CommandText = "SELECT " + ID_Name + " FROM " + TableName + " WHERE rowid=" + i.ToString();
    i = cmd.ExecuteScalar();
    
    0 讨论(0)
  • 2020-11-30 07:59
        database = new SQLiteConnection(databasePath);
    
        public int GetLastInsertId()
        {
            return (int)SQLite3.LastInsertRowid(database.Handle);
        }
    
    0 讨论(0)
  • 2020-11-30 08:01

    The SQLiteConnection object has a property for that, so there is not need for additional query. After INSERT you just my use LastInsertRowId property of your SQLiteConnection object that was used for INSERT command. Type of LastInsertRowId property is Int64. Off course, as you already now, for auto increment to work the primary key on table must be set to be AUTOINCREMENT field, which is another topic.

    0 讨论(0)
  • 2020-11-30 08:03

    Using C# (.net 4.0) with SQLite, the SQLiteConnection class has a property LastInsertRowId that equals the Primary Integer Key of the most recently inserted (or updated) element.

    The rowID is returned if the table doesn't have a primary integer key (in this case the rowID is column is automatically created).

    See https://www.sqlite.org/c3ref/last_insert_rowid.html for more.

    As for wrapping multiple commands in a single transaction, any commands entered after the transaction begins and before it is committed are part of one transaction.

    long rowID;
    using (SQLiteConnection con = new SQLiteConnection([datasource])
    {
        SQLiteTransaction transaction = null;
        transaction = con.BeginTransaction();
    
        ... [execute insert statement]
    
        rowID = con.LastInsertRowId;
    
        transaction.Commit()
    }
    
    0 讨论(0)
  • 2020-11-30 08:04

    I'm using Microsoft.Data.Sqlite package and I do not see a LastInsertRowId property. But you don't have to create a second trip to database to get the last id. Instead, combine both sql statements into a single string.

            string sql = "insert into MyTable values (null, @name);  select last_insert_rowid();";
            using (var cmd = conn.CreateCommand()) {
                cmd.CommandText = sql;
                cmd.Parameters.Add("@name", SqliteType.Text).Value = "John";
                int lastId = Convert.ToInt32(cmd.ExecuteScalar());
            }
    
    0 讨论(0)
  • 2020-11-30 08:10
    select last_insert_rowid();
    

    And you will need to execute it as a scalar query.

    string sql = @"select last_insert_rowid()";
    long lastId = (long)command.ExecuteScalar(sql); // Need to type-cast since `ExecuteScalar` returns an object.
    
    0 讨论(0)
提交回复
热议问题