asp.net sqldatasource with postgres select parameters

时间:2019-03-17 22:36:58

标签: asp.net postgresql sqldatasource

In Visual Studio I have an asp.net (vb.net) project using a sqldatasource component connecting to a postgres database. This works fine but now I need to provide a parameter to my select statement and I find no solution:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
ProviderName="Npgsql"
SelectCommand="SELECT column2 from myTable where column1 = :column1)">
 <SelectParameters>
  <asp:Parameter Name=":column1" Type="String"/>
 </SelectParameters>
</asp:SqlDataSource>

When I try to update the scheme I get an

Error 42601 Syntax error at >>:<<

I also tried using parameter @column1 (instead of :column1, which works for my mssqlserver connections), but then I get

Error 42883 operator does not exist @ character varying".

Is there a way to use the sqldatasource with postgres sql parameters without programming code behind?

1 个答案:

答案 0 :(得分:0)

Meanwile我或多或少偶然地找到了解决方案,所以如果有人对此感兴趣,我可以在这里提供: 在SelectCommdand中,我使用“ @ column1”作为参数。 在SelectParamter部分中,参数名称必须为“ column1”(不带@):

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
ProviderName="Npgsql"
SelectCommand="SELECT column2 from myTable where column1 = @column1)">
 <SelectParameters>
  <asp:Parameter Name="column1" Type="String"/>
 </SelectParameters>
</asp:SqlDataSource>

仍然无法使用sqldatasource组件执行“更新方案”(仍然出现错误),但是现在select语句在运行时可以正常运行,即基于sqldatasource的gridview根据需要显示所选数据。