SqlCommand cmd = new SqlCommand("INSERT INTO Users (" +
"LoginName," +
"FirstName," +
"LastName," +
"Password," +
"Email," +
"DOB," +
"Sex" +
") VALUES (" +
"@Email," +
"@FirstName," +
"@LastName," +
"@Password," +
"@Email," +
"@DOB," +
"@Sex)" +
" Select Scope_Identity();",conn);
Here is how you would execute the query:
int UserId = (int)cmd.ExecuteScalar();
Most of the time you will need to use the Scope_Identity() when you have to deal with foreign key constraints, that's why a Users table is a good example.
No comments:
Post a Comment