Donate

Retrieve Last Inserted Record In MySQL Database Using C#.NET

To get last inserted unique ID in MySQL, use LAST_INSERT_ID().
C# Code:
1
2
3
4
string insert_product_query = ("INSERT INTO PRODUCTS (Stock) VALUES (5); SELECT LAST_INSERT_ID()");
MySqlCommand cmd_query = new MySqlCommand(insert_product_query, objConn);
int idResult = Convert.ToInt32(cmd_query.ExecuteScalar());
txtID.Text = idResult.ToString();
MySQL Docs: Getting Last Unique ID in MySQL

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.