Search code examples
mysqlpowershelllast-insert-id

last_insert_id in Powershell


In Powershell, I have a simple Mysql insert command.

$query = "INSERT INTO TABLE1 (id, col1, col2) VALUES (1, 'two', 'three');"<br>
$command = New-Object MySql.Data.MySqlClient.MySqlCommand($query, $connection)
$command.ExecuteNonQuery()

I'm using Mysql Connector Net 6.2.4.

Is it possible to return the last_insert_id() or something similar?

I've looked here for some info but can't really find anything.

Any thoughts?


Solution

  • Isn't there a $command.LastInsertedId property? If not you can always issue a SELECT LAST_INSERT_ID() query.