【发布时间】:2020-01-28 20:17:18
【问题描述】:
我正在尝试编写一个选择语句 (postgreSQL),它使用我的应用程序屏幕中的一个字段。
我已经 2 天没有成功。 任何帮助和/或解释等......将不胜感激。
下面是整个子。 (代码尚未完成、优化或测试,请原谅任何糟糕的编码) 在以下行收到错误:ODBCdaDups.Fill(dsDups2) 收到的错误是:ERROR [42883] ERROR: operator does not exist:@text; & vblf & "执行查询时出错" 我尝试使用编号参数($1、$2 等),但也无法弄清楚。
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
【问题讨论】:
-
创建与查询同名的参数:
cmdDups.Parameters.Add("@auction_name, SqlDbType.Text).Value = ...`
标签: sql vb.net postgresql parameters npgsql