See Also

MySqlCommand Class  | MySqlCommand Members

 

Language

Visual Basic

C#

Show All

See AlsoRequirementsLanguagesDevart.Data.MySqlSend comments on this topic.

InsertId Property

Returns the ID generated for an AUTO_INCREMENT column by the previous command.

[Visual Basic]
Public ReadOnly Property InsertId As Long
[C#]
public long InsertId {get;}

Return Type

The ID generated for an AUTO_INCREMENT column.

Remarks

Use InsertId property after you have performed an INSERT command into a table that contains an AUTO_INCREMENT field.

If the command doesn't perform an insertion into a table that contains an AUTO_INCREMENT field the value of InsertId won't be defined.

When several rows are inserted, value of InsertId depends on FetchAll property. If FetchAll property is false, InsertId returns value for the first inserted row, otherwise it returns value for the last inserted row.

Example

In the following example an insert operation is performed assuming that DeptNo column in Test.Dept table has Autoincrement attribute. After running the query the value assigned to DeptNo for the inserted record is shown.

[C#] 

static void GetLastId(MySqlConnection myConnection) 

  MySqlCommand myCommand = new MySqlCommand("INSERT INTO Test.Dept (DeptNo, DName, Loc) VALUES (Null, 'AnotherOne', 'SomeWhere')", myConnection); 
  myConnection.Open(); 
  try 
  { 
    myCommand.ExecuteNonQuery(); 
    Console.WriteLine(myCommand.InsertId); 
  } 
  finally 
  { 
    myConnection.Close(); 
  } 
}

[Visual Basic] 

Public Sub GetLastId(ByVal myConnection As MySqlConnection)
  Dim myCommand As New MySqlCommand("INSERT INTO Test.Dept (DeptNo, DName, Loc) VALUES (Null, 'AnotherOne', 'SomeWhere')", myConnection)
  myConnection.Open()
  Try
    myCommand.ExecuteNonQuery()
    Console.WriteLine(myCommand.InsertId)
  Finally
    myConnection.Close()
  End Try
End Sub

 

See Also

MySqlCommand Class  | MySqlCommand Members

 

 


© 2002 - 2008 Devart. All Rights Reserved.