更新查询不工作vb net

时间:2018-01-14 20:44:26

标签: sql vb.net postgresql npgsql

当我尝试使用以下查询更新数据库时,收到消息“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 个答案:

答案 0 :(得分:1)

修复代码

尝试以下代码,(1)我使用cmd.ExecuteNonQuery()代替Conexao.NonQuery(cd),(2)query字符串未分配给cmd命令,(3)在postgresql中没有使用[]括号,如果需要,你必须使用双引号。 (4)在使用npgsqlcommand Class

时,建议使用Using
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注入)以及更好的性能,您可以按照这些类似的问题获取更多信息:

<强>参考