Paulo_Rocha Posted July 4, 2012 at 04:17 PM Report Share #467287 Posted July 4, 2012 at 04:17 PM Boas pessoal, Eu necessito de saber o ID da linha que estou a inserir na base de dados. imaginemos que eu estou a utilizar algo do genero : OleDbConnection Connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Application.StartupPath + "\\BD.mdb"); string query = "INSERT INTO Clientes([Nome], [Empresa]) VALUES (@Nome, @Empresa)"; OleDbCommand command = new OleDbCommand(query, Connection); command.Parameters.Add("@Nome", OleDbType.VarChar).Value = NomeTextbox.Text; command.Parameters.Add("@Empresa", OleDbType.VarChar).Value = EmpresaTextbox.Text; Connection.Open(); int x = command.ExecuteNonQuery(); if (x < 1) { MessageBox.Show("Erro ao inserir", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("Registo inserido com sucesso!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } Connection.Close(); command = null; Como consigo saber o ID? Link to comment Share on other sites More sharing options...
KiNgPiTo Posted July 4, 2012 at 04:27 PM Report Share #467288 Posted July 4, 2012 at 04:27 PM (edited) Não percebo muito de c# mas com uma simples pesquisa cheguei a isto: string query = "INSERT INTO Clientes([Nome], [Empresa]) OUTPUT INSERTED.ID VALUES (@Nome, @Empresa)"; E a seguir ao ExecuteNonQuery: int id = (int)command.ExecuteScalar(); Edited July 4, 2012 at 04:32 PM by KiNgPiTo Link to comment Share on other sites More sharing options...
Paulo_Rocha Posted July 4, 2012 at 07:17 PM Author Report Share #467318 Posted July 4, 2012 at 07:17 PM tá boa 🙂 obrigado eu estava a abrir a base de dados de novo e procurar pela linha pretendida e ler o ID, Link to comment Share on other sites More sharing options...
Paulo_Rocha Posted July 5, 2012 at 08:11 AM Author Report Share #467358 Posted July 5, 2012 at 08:11 AM Boas kingpito, Em relação ao código que enviaste já testei e não está a funcionar aquela parte da query dá erro. no entanto encontrei uma outra solução que já testei e funciona. cmdIns.Parameters.Clear(); cmdIns.CommandText = "SELECT @@IDENTITY"; // Get the last inserted id. int insertID = Convert.ToInt32( cmdIns.ExecuteScalar() ); Obrigado Cumprimentos Paulo Rocha Link to comment Share on other sites More sharing options...
Rechousa Posted August 26, 2012 at 10:04 PM Report Share #473606 Posted August 26, 2012 at 10:04 PM Olá, Atenção que @@IDENTITY pode retornar valores que poderão não ser os que pretendes. Sugiro a leitura deste artigo: http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/ Pedro Martins Sharing is Knowledge! http://www.linkedin.com/in/rechousa Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now