【问题标题】:How to specify parameter value for stored procedure in SqlDataSource如何在 SqlDataSource 中为存储过程指定参数值
【发布时间】:2011-07-16 14:46:24
【问题描述】:

刚开始使用 SqlDataSource 的声明式语法,我试图找出一种将参数值设置为存储过程的方法。我有一个通过请求对象传递的 Client_ID,我需要在执行 SqlDataSource 的存储过程之前设置 Client_ID。

我有几个问题。

  1. 存储过程参数是否必须在 ASPX 标记中预定义,还是可以在代码隐藏中动态添加?

  2. 谁有一个例子来演示带有存储过程和参数的 SqlDataSource 标记以及在代码隐藏中设置该参数值吗?

【问题讨论】:

    标签: asp.net stored-procedures parameters sqldatasource


    【解决方案1】:

    这是一个使用 SqlDataSource 的参数化查询(存储过程和文本)的相当详尽的示例,包括以编程方式设置参数。 ASP.NET - Using Parameterized Queries with the SqlDataSource

    【讨论】:

    • 虽然此链接可能会回答问题,但最好包含答案的基本部分here 并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    【解决方案2】:

    使用 .net 4 框架,您可以...

    1.它可以动态添加,但您必须提供自己的代码表达式生成器(更多信息see here) 1.1 你也可以使用不同的参数来实现相同的目标,比如:

    2.

      <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%$ ConnectionStrings:yourConnectionString %>" 
      SelectCommand="YourStoreProcedure" SelectCommandType="StoredProcedure">
      <SelectParameters>
         <asp:ControlParameter Name="yourParameterName" ControlID="controlThatHoldsParameterValue" PropertyName="Text" />
      </SelectParameters>
    

    【讨论】:

      【解决方案3】:
       <asp:SqlDataSource ID="ADSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ADConnection %>"
          SelectCommand="GetProfile" SelectCommandType="StoredProcedure">
          <SelectParameters>
              <asp:ControlParameter ControlID="InputTextBox" Name="Host" PropertyName="Text" Type="String" />
          </SelectParameters>
      </asp:SqlDataSource>
      

      GetProfile 是存储的过程名称,Host 是参数名称,它是从一个名为 InputTextBox 的 texbox 中获取的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多