Dim MySQL As String = "Insert into Groups(UserID,GroupName) " & _
"Values(@userid,@groupname); SELECT @@ IDENTITY;"
Cmd = New SqlCommand(MySQL, SqlConnection1)
With Cmd.Parameters
.Add(New SqlParameter("@userid", UserID))
.Add(New SqlParameter("@groupname", TextBox1.Text))
End With
SqlConnection1.Open()
Try
newID = Cmd.ExecuteScalar()
Catch
End Try
SqlConnection1.Close()
This runs the insert AND the select to retrieve the last identity. None of this is a function of ADO.NET, but of SQL. As I stated earlier this will work in MS-SQL Server. You will need to use other syntax (if available) for other database engines. If you aren't using MSSQL, then I can't answer the question. This is how I always do this because my inserts are usually made with a straight SQL command execution. There will be other methods when you are using datasets and use the methods of the dataset class to insert data.
|