SCOPE_IDENTITY
is a SQL Server function. You need the equivalent function for MySQL, last_insert_id
:
SqlCommand cmd = new SqlCommand("insert into solicitacoes (assunto, mensagem, endereco, anexo, status, id_Departamento) values ('" + valorcadastro.assunto + "', '" + valorcadastro.mensagem + "', '" + valorcadastro.endereco + "','" + valorcadastro.anexo + "','Inicial', 0);SELECT last_insert_id()", con);
cmd.ExecuteNonQuery();
int ultimoRegistro = cmd.ExecuteScalar();
Note that you need to use ExecuteScalar
instead of ExecuteNonQuery
to get the value returned by the DB.
Or, if you use the class MySqlCommand
, you can access the property LastInsertedId
.
Note that your command is open for SQL injection attacks. You should use parameterized queries:
var cmd = new SqlCommand("insert into solicitacoes (assunto, mensagem, endereco, anexo, status, id_Departamento) values ('@assunto', '@mensagem', '@endereco','@anexo','Inicial', 0);SELECT last_insert_id()", con);
cmd.Parameters.AddWithValue("@assunto", valorcadastro.assunto);
cmd.Parameters.AddWithValue("@mensagem", valorcadastro.mensagem);
cmd.Parameters.AddWithValue("@endereco", valorcadastro.endereco);
cmd.Parameters.AddWithValue("@anexo", valorcadastro.anexo);