williamjda Posted October 6, 2012 at 03:28 PM Report #477999 Posted October 6, 2012 at 03:28 PM (edited) Amigo estou com o seguinte problema. Criei um form onde os dados são exibidos em um datagrid. Consigo gravar, filtrar porem não estou conseguindo excluir os dados do meu banco de dados. Pois esta dando a seguinte mensagem: Uma expressão de tipo não booleano especificada em um contexto no qual se espera uma condição, próximo a ','. Na seguinte linha: Dim x As Integer = command.ExecuteNonQuery() O código do botão remover é este: Private Sub BT_Excluir_Click(sender As System.Object, e As System.EventArgs) Handles BT_Excluir.Click ' Texto de ligação à base de dados Dim myConnectionString As String = "Data Source=localhost;Initial Catalog=BDSOSPET;Integrated Security=True;User ID=sa;Password=senha" ' Comando que irá apagar todos os dados na tabela "MyTable" em que o campo ' "username" é igual ao parâmetros posteriormente indicado Dim SQL As String = "DELETE FROM CadastroClientes WHERE ([Nome], [RG], [CPF], [Telefone], [Celular], [CEP], [Cidade], [Estado], [bairro], [Rua], [Numero], [Complemento], [Data]) VALUES (@Nome, @RG, @CPF, @Telefone, @Celular, @CEP, @Cidade, @Estado, @Bairro, @Rua, @Numero, @Complemento, @Data);" ' Cria uma nova ligação à base de dados Dim connection As New SqlConnection(myConnectionString) ' Criação do comando indicando a instrução e a ligação Dim command As New SqlCommand(SQL, connection) ' Indicação do parâmetro que irá ser apagado command.Parameters.Add("@Nome", SqlDbType.NVarChar).Value = NomeTextBox.Text command.Parameters.Add("@RG", SqlDbType.NVarChar).Value = RGMaskedTextBox.Text command.Parameters.Add("@CPF", SqlDbType.NVarChar).Value = CPFMaskedTextBox.Text command.Parameters.Add("@Telefone", SqlDbType.NVarChar).Value = TelefoneMaskedTextBox.Text command.Parameters.Add("@Celular", SqlDbType.NVarChar).Value = CelularMaskedTextBox.Text command.Parameters.Add("@CEP", SqlDbType.NVarChar).Value = CEPMaskedTextBox.Text command.Parameters.Add("@Cidade", SqlDbType.NVarChar).Value = CidadeTextBox.Text command.Parameters.Add("@Estado", SqlDbType.NVarChar).Value = EstadoComboBox.Text command.Parameters.Add("@Bairro", SqlDbType.NVarChar).Value = BairroTextBox.Text command.Parameters.Add("@Rua", SqlDbType.NVarChar).Value = RuaTextBox.Text command.Parameters.Add("@ComplementoTextBox", SqlDbType.NVarChar).Value = ComplementoTextBox.Text command.Parameters.Add("@DataTextBox", SqlDbType.NVarChar).Value = DataTextBox.Text ' Abre a ligação, executa o comando e guarda em "x" o número de registos eliminados connection.Open() Dim x As Integer = command.ExecuteNonQuery() If x < 1 Then MessageBox.Show("A operação efectuada não retomou qualquer resultado.") End If ' Fecha a ligação e limpa as variáveis connection.Close() connection = Nothing command = Nothing End Sub Poderiam me ajudar quanto a esta questão?Muito obrigado. Edited October 6, 2012 at 03:29 PM by williamjda
renafi Posted October 6, 2012 at 07:39 PM Report #478052 Posted October 6, 2012 at 07:39 PM Não é assim que fazes uma condição DELETE. ex: delete from CastroClientes where Nome = @Nome and RG = @RG ... e por aí adiante Oracle Certified Professional - AdministraçãoOracle Certified Professional - Pl/sqlMCPD - Microsoft Certified Professional DeveloperMCTS - Microsoft Certified Technology Specialist
acao Posted October 6, 2012 at 09:06 PM Report #478083 Posted October 6, 2012 at 09:06 PM (edited) boas na minha modesta opinião deveria ser atraves da chave primária ex: "DELETE FROM CadastroClientes WHERE CodId = @CodId cumps acao Edited October 6, 2012 at 09:07 PM by acao
williamjda Posted October 7, 2012 at 03:12 AM Author Report #478103 Posted October 7, 2012 at 03:12 AM Amigo com o código abaixo ele simplesmente não apaga. Ele retorna a mensagem que "A operação efetuada não retomou nenhum resultado". Dim SQL As String = "DELETE FROM CadastroClientes WHERE Nome = @Nome and RG = @RG and CPF = @CPF and Telefone = @Telefone and Celular = @Celular and CEP = @CEP and Cidade = @Cidade and Estado = @Estado and Bairro = @Bairro and Rua = @Rua and Numero = @Numero and Complemento = @Complemento and Data = @Data" E com o código abaixo, tenho a seguinte mensagem: Sintaxe incorreta próxima a ','. Dim SQL As String = "DELETE FROM CadastroClientes WHERE Nome = @Nome, RG = @RG, CPF = @CPF, Telefone = @Telefone, Celular = @Celular, CEP = @CEP, Cidade = @Cidade, Estado = @Estado, Bairro = @Bairro, Rua = @Rua, Numero = @Numero, Complemento = @Complemento, Data = @Data" Sei que devo esta fazendo alguma coisa errada porem você poderiam me ajudar visto que não sou nenhum Expert em SQL. Grato
bioshock Posted October 7, 2012 at 08:53 AM Report #478108 Posted October 7, 2012 at 08:53 AM Falta-te declarar o campo "Numero" nos parâmetros. Tens 13 campos para declarar, e, no código que mostraste no teu 1º post, só tens 12 campos. https://wiki.portugal-a-programar.pt/dev_net/vb.net/access/
williamjda Posted October 8, 2012 at 07:35 PM Author Report #478311 Posted October 8, 2012 at 07:35 PM Bom amigos consegui fazer o código funcionar de maneira bem simples e bem amadora mas funcionou. If MsgBox("Tem Certeza que deseja Exluir este Registro?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then Me.CadastroLoginBindingSource.RemoveCurrent() Me.TableAdapterManager.UpdateAll(Me.BDSOSANIMALDataSet) BT_Alterar.Enabled = False BT_Cancelar.Enabled = False BT_Excluir.Enabled = False BT_Gravar.Enabled = False BT_Novo.Enabled = True Else Me.Validate() Me.CadastroLoginBindingSource.EndEdit() Me.TableAdapterManager.UpdateAll(Me.BDSOSANIMALDataSet) End If BT_Novo.Focus() End Sub
jpaulino Posted October 8, 2012 at 08:33 PM Report #478317 Posted October 8, 2012 at 08:33 PM Viste o comentário do @acao? É assim que deves fazer. boas na minha modesta opinião deveria ser atraves da chave primária ex: "DELETE FROM CadastroClientes WHERE CodId = @CodId cumps acao
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