Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

sql

sqlite

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

kompenzacijeDataSet.KompenzacijeRow kompenzacija = kompenzacijeDataSet.Kompenzacije.NewKompenzacijeRow(); kompenzacija.Datum = DateTime.Now; kompenzacija.PodjetjeID = stranka.id; kompenzacija.Znesek = Decimal.Parse(tbZnesek.Text);  kompenzacijeDataSet.Kompenzacije.Rows.Add(kompenzacija);  kompenzacijeDataSetTableAdapters.KompenzacijeTableAdapter kompTA = new kompenzacijeDataSetTableAdapters.KompenzacijeTableAdapter(); kompTA.Update(this.kompenzacijeDataSet.Kompenzacije);  this.currentKompenzacijaID = LastInsertID(kompTA.Connection); 

The last line is important. Why do I supply a connection? Well there is a SQLite function called last_insert_rowid() that you can call and get the last insert ID. Problem is it is bound to a connection and .NET seems to be reopening and closing connections for every dataset operation. I thought getting the connection from a table adapter would change things. But it doesn't.

Would anyone know how to solve this? Maybe where to get a constant connection from? Or maybe something more elegant?

Thank you.

EDIT:

This is also a problem with transactions, I would need the same connection if I would want to use transactions, so that is also a problem...

like image 220
mishmash Avatar asked Dec 02 '10 23:12

mishmash


People also ask

How to get last inserted ID in SQLite c#?

SQLite has a special SQL function – last_insert_rowid() – that returns the ID of the last row inserted into the database so getting the ID of a new row after performing a SQL insert just involves executing the last_insert_rowid() command.

How do I find the last row ID in SQLite?

In order to access this database, you don't need to establish any kind of connections for it like JDBC, ODBC etc. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I find the last inserted record id?

If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL.

What does SQLite insert return?

In the INSERT statement above, SQLite computes the values for all three columns. The RETURNING clause causes SQLite to report the chosen values back to the application. This saves the application from having to issue a separate query to figure out exactly what values were inserted.


2 Answers

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() } 
like image 78
Alex Smith Avatar answered Sep 20 '22 23:09

Alex Smith


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. 
like image 38
Will Marcouiller Avatar answered Sep 22 '22 23:09

Will Marcouiller