2

I am having a problem with my database problem.. errr..

INSERT INTO Customer (CustomerName)
VALUES ('Joe Schmoe'); LAST_INSERT_ID();

I used something like that in my winform so this statement:

  INSERT INTO Customer (CustomerName)
  VALUES ('Joe Schmoe'); LAST_INSERT_ID();

-shows me the last inserted id in my customer column

I want to store the value that I can get into a variable so that whenever I put a value to its child table (foreign key) I will use what value that I have stored in my variable to my insert statement.

How can I store it in a variable or a control?

MPelletier
  • 15,673
  • 14
  • 84
  • 131
rj tubera
  • 727
  • 4
  • 28
  • 51

1 Answers1

2

Look at below examples. Cannot give the exact SQL without knowing your database.

SQL Server and C#: get last inserted id

Get inserted id.

How to get last inserted id?

Getting the identity of the most recently added record

C# Code Snippet - Get Identity Column Value from SQL Server

EDIT

If it is MySQL Database, you could use LAST_INSERT_ID() built in function.

Here is a code sample.

Community
  • 1
  • 1
CharithJ
  • 44,463
  • 20
  • 111
  • 128