简体   繁体   中英

postgreSQL query with parameter from vb.net

I am trying to write a select statement (postgreSQL) which uses a field from my app screen.

I have been unsuccessful for 2 days. Any help and/or explanations etc... would be greatly appreciated.

Below is the entire sub. (The code is not completed, optimized, or tested yet so forgive any bad coding please) The error is received on the following line: ODBCdaDups.Fill(dsDups2) The error received is: ERROR [42883] ERROR: operator does not exist: @ text; & vblf & "Error while executing the query" I tried using numbered parameters ($1, $2, etc...) but could not figure it out either.

Private Sub Check4Duplicate()
    Dim DupMessage As String
    Try
        Dim DupSQL As String = Nothing
        Dim DupConn As New OdbcConnection()
        Dim strConn As String = ""
        If GlobalVariables.logProd = 1 Then
            strConn = "Driver={PostgreSQL ANSI};database=SacredSelections;server=127.0.0.1;port=5432;uid=SSApp;sslmode=disable;readonly=0;protocol=7.4;User ID=SSApp;password=Cordova123;"
        Else
            strConn = "Driver={PostgreSQL ANSI};database=SacredSelectionsTest;server=127.0.0.1;port=5432;uid=SSApp;sslmode=disable;readonly=0;protocol=7.4;User ID=SSApp;password=Cordova123;"
        End If
        Dim dsDups2 As New DataSet
        Dim ODBCdaDups As OdbcDataAdapter
        Dim cmdbldDups As OdbcCommandBuilder
        Dim cmdDups As OdbcCommand
        DupConn = New OdbcConnection(strConn)
        DupConn.Open()
        dsDups2 = New DataSet
        ODBCdaDups = New OdbcDataAdapter
        cmdbldDups = New OdbcCommandBuilder(ODBCdaDups)
        dsDups2.DataSetName = "auctions"

        ' Create the SelectCommand.
        ' Original SQL Server command
        'cmdDups = New OdbcCommand("Select * From auctions where auction_name = @auction_name;", DupConn)
        ' Trying to create new postgresql command
        cmdDups = New OdbcCommand("do $$
        begin
          select *
          from auctions
          where auction_name = @auction_name;
        end;
        $$
        ", DupConn)
        cmdDups.Parameters.Add("auction_name", SqlDbType.Text).Value = txt_auction_name.Text
        cmdDups.Prepare()
        ODBCdaDups.SelectCommand = cmdDups
        ODBCdaDups.Fill(dsDups2)
        DupConn.Close()

        If Not DupConn Is Nothing Then DupConn.Dispose()

        DupMessage = ""
        ' Loop over tables in the DataSet.
        Dim collection As DataTableCollection = dsDups2.Tables
        If dsDups2.Tables(0).Rows.Count = 0 Then
            DupTitle = "Unique Record - Save Allowed"
            If strEditType = "Edit" Then
                DupMessage = "This will save changes to an existing Unique record for Auction ID " + txt_auction_id.Text + " and Auction Name " + txt_auction_name.Text + " and Location Name " + txt_location_address.Text
            Else
                DupMessage = "This will create a Unique record for Auction ID " + txt_auction_id.Text + " and Auction Name " + txt_auction_name.Text + " and Location Name " + txt_location_address.Text
            End If
        Else
            DupTitle = "Duplicate Record - Save NOT Allowed"
            DupMessage = "A record already exists with Auction ID " + txt_auction_id.Text + " and Auction Name " + txt_auction_name.Text + " and Location Name " + txt_location_address.Text
            MessageBox.Show(DupMessage, DupTitle)
        End If

    Catch ex As Exception
        DupTitle = "Connection failed"
        DupMessage = "Unable to Open Auction Information Connection to check for Existing Records"
        MessageBox.Show(DupMessage, DupTitle)
    End Try
End Sub

Names of parameters in ODBC do not matter. What matters is the order that the parameters appear in the sql command must match the order which they are added to the ParametersCollection .

Using...End Using blocks close and dispose database objects even if there is an error.

I had to guess the field names and datatypes so, check you database for the actual values and adjust your code accordingly. If any of the fields are numeric types you will not to convert the textbox's .Text property to the proper type.

Normally Primary Key fields would never be updated so delete the update of the PK field from the code. If a field is auto incremented you would not insert a value for that field. Adjust the code by removing any insert to the auto number field.

Private Function GetConnectionString() As String
    Dim strConn As String
    If GlobalVariables.logProd = 1 Then
        strConn = "Driver={PostgreSQL ANSI};database=SacredSelections;server=127.0.0.1;port=5432;uid=SSApp;sslmode=disable;readonly=0;protocol=7.4;User ID=SSApp;password=Cordova123;"
    Else
        strConn = "Driver={PostgreSQL ANSI};database=SacredSelectionsTest;server=127.0.0.1;port=5432;uid=SSApp;sslmode=disable;readonly=0;protocol=7.4;User ID=SSApp;password=Cordova123;"
    End If
    Return strConn
End Function

Private Sub Check4Duplicate(strEditType As String)
    Dim DupMessage As String
    Dim DupTitle As String
    Try
        Dim dt As New DataTable
        Using DupConn As New OdbcConnection(GetConnectionString())
            Using cmdDups As New OdbcCommand("select auction_name from auctions where auction_name = ?;", DupConn)
                cmdDups.Parameters.Add("@auction_name", OdbcType.VarChar).Value = txt_auction_name.Text
                dt.Load(cmdDups.ExecuteReader)
            End Using
        End Using

        If dt.Rows.Count = 0 Then
            DupTitle = "Unique Record - Save Allowed"
            If strEditType = "Edit" Then
                DupMessage = "This will save changes to an existing Unique record for Auction ID " + txt_auction_id.Text + " and Auction Name " + txt_auction_name.Text + " and Location Name " + txt_location_address.Text
                UpdateAuction()
            Else
                DupMessage = "This will create a Unique record for Auction ID " + txt_auction_id.Text + " and Auction Name " + txt_auction_name.Text + " and Location Name " + txt_location_address.Text
                InsertAuction()
            End If
        Else
            DupTitle = "Duplicate Record - Save NOT Allowed"
            DupMessage = $"A record already exists with Auction ID {txt_auction_id.Text} and Auction Name {txt_auction_name.Text} and Location Name {txt_location_address.Text}"
            MessageBox.Show(DupMessage, DupTitle)
        End If
    Catch ex As Exception
        DupTitle = "Connection failed"
        DupMessage = "Unable to Open Auction Information Connection to check for Existing Records"
        MessageBox.Show(DupMessage, DupTitle)
    End Try
End Sub

Private Sub InsertAuction()
    Using DupConn As New OdbcConnection(GetConnectionString())
        Using cmd As New OdbcCommand("Insert Into auctions (auction_id, auction_name, auction_address) Values (?,?,?)")
            cmd.Parameters.Add("id", OdbcType.VarChar).Value = txt_auction_id.Text
            cmd.Parameters.Add("name", OdbcType.VarChar).Value = txt_auction_name.Text
            cmd.Parameters.Add("address", OdbcType.VarChar).Value = txt_location_address.Text
            DupConn.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using
End Sub

Private Sub UpdateAuction()
    Using DupConn As New OdbcConnection(GetConnectionString())
        Using cmd As New OdbcCommand("Update auctions Set auction_id = ?, auction_name = ?, auction_address = ? Where aution_name = ?")
            cmd.Parameters.Add("id", OdbcType.VarChar).Value = txt_auction_id.Text
            cmd.Parameters.Add("name", OdbcType.VarChar).Value = txt_auction_name.Text
            cmd.Parameters.Add("address", OdbcType.VarChar).Value = txt_location_address.Text
            cmd.Parameters.Add("name2", OdbcType.VarChar).Value = txt_auction_name.Text
            DupConn.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using
End Sub

It may seem like duplication of code repeating the construction of connections and commands but keeping these items local to the method where they are used allows us to be sure they are closed and disposed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM