更新查询无效 vb 净

update query not working vb net

当我尝试使用以下查询更新数据库时,我收到消息 "update successful",但这不是真的,我的数据仍然和以前一样.

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

我的查询有什么问题?

修复您的代码

尝试以下代码,(1) 我使用 cmd.ExecuteNonQuery() 而不是 Conexao.NonQuery(cd) 也 (2) query 字符串未分配给 cmd 命令,并且(3) [] 括号在 postgresql 中没有使用,如果需要你必须使用双引号。 (4) 建议在使用npgsql命令时使用UsingClass

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

参数化查询

建议使用参数化查询以获得更好的安全性(更安全地防止 sql 注入) 和更好的性能,您可以关注这些类似的问题以获取更多信息:

  • Update command using Npgsql postgres and C#

参考资料