Reputation: 701
When I try to update the database using the following query, I receive the message "update successful", but it's not true, my data is still the same as before.
Public Function alterar() As Boolean
Dim cd As New npgSqlCommand
Dim query As String = ""
Try
query &= "UPDATE [Caixa] SET Recibo = '" & iRecibo & "' , CentroCusto = '" & sCentroCusto & "' , Data = '" & sData & "' , Dia = '" & sDia & "' , _
Mes ='" & sMes & "', Ano = '" & sAno & "' , CentroCustoResumo = '" & sCentroCustoResumo & "' , CodSubCentroCusto ='" & sCodSubCentroCusto & "', _
DescSubCentroCusto ='" & sDescSubCentroCusto & "', Eventos ='" & sEventos & "', Historico = '" & sHistorico & "' , _
Dinheiro = '" & sDinheiro & "', Cheque = '" & sCheque & "', Cartao = '" & sCartao & "', DepositoDireto = '" & sDepositoDireto & "', _
Total ='" & sTotal & "' WHERE Recibo = '" & iRecibo.ToString()
If Conexao.NonQuery(cd) Then
sStatus = "O Recibo " & iRecibo & " foi alterado com sucesso!"
Return True
End If
Catch ex As Exception
sStatus = ex.Message
End Try
Return True
End Function
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim c As New Caixa()
Try
If TextBox1.Text <> "" Then
c.Recibo = CInt(TextBox1.Text)
If c.excluir() Then
MessageBox.Show(c.Status)
CaixaLoad()
Else
MessageBox.Show(c.Status)
End If
Else
MessageBox.Show("Nenhum registro selecionado!")
End If
Catch ex As Exception
'MessageBox.Show(ex.Message)
End Try
End Sub
What's the problem with my query?
Upvotes: 0
Views: 470
Reputation: 37358
Try the following code, (1) i used cmd.ExecuteNonQuery()
instead of Conexao.NonQuery(cd)
also (2) the query
string was not assigned to cmd
command, and (3) []
brackets are not used in postgresql you have to use double quotes if needed. (4) it is recommended to use Using
when working with npgsqlcommand Class
Public Function alterar() As Boolean
Dim query As String = ""
Try
query = "UPDATE Caixa SET Recibo = '" & iRecibo & "' , CentroCusto = '" & sCentroCusto & "' , Data = '" & sData & "' , Dia = '" & sDia & "' , " & _
"Mes ='" & sMes & "', Ano = '" & sAno & "' , CentroCustoResumo = '" & sCentroCustoResumo & "' , CodSubCentroCusto ='" & sCodSubCentroCusto & "', " & _
"DescSubCentroCusto ='" & sDescSubCentroCusto & "', Eventos ='" & sEventos & "', Historico = '" & sHistorico & "' , " & _
"Dinheiro = '" & sDinheiro & "', Cheque = '" & sCheque & "', Cartao = '" & sCartao & "', DepositoDireto = '" & sDepositoDireto & "', " & _
"Total ='" & sTotal & "' WHERE Recibo = '" & iRecibo.ToString & "'"
Using cmd As New npgSqlCommand
cmd.CommandText = query
cmd.Connection = conn
cmd.ExecuteNonQuery()
sStatus = "O Recibo " & iRecibo & " foi alterado com sucesso!"
End Using
Return True
Catch ex As Exception
sStatus = ex.Message
Return False
End Try
End Function
it is recommended to use parameterized queries for better security (more secure against sql injection) and better performance, you can follow these similar question for more information:
References
Upvotes: 1