如何在SqlDataSource中为存储过程指定参数值

发布于 2024-10-22 14:21:17 字数 281 浏览 4 评论 0原文

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

我有几个问题。

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

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

Being new to using the declarative syntax of SqlDataSource I am trying to figure out a way to set the value of a parameter to a stored procedure. I have a Client_ID that is passed via the Request Object and I need to set the Client_ID before the stored procedure of the SqlDataSource is executed.

I have a couple of questions.

  1. Does the stored procedure parameter have to be predefined in the ASPX markup or can it be added dynamically in the code-behind?

  2. Would anyone have an example that demonstrates the SqlDataSource markup with a stored procedure and parameter as well as setting that parameter value in code-behind?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧伤还要旧人安 2024-10-29 14:21:17

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

1.它可以动态添加,但您必须提供自己的代码表达式生成器(有关更多信息参见此处
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>

With .net 4 framework you can ...

1.It can be added dynamically, but you would have to provide your own code expression builder (for further info see here)
1.1 You can also use different parameter to achieve same goal, like :

enter image description here

2.

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

独行侠 2024-10-29 14:21:17

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

This is a fairly thorough example of using parameterized queries (Stored Procedure and Text) with SqlDataSource, including programmatically setting parameters. ASP.NET - Using Parameterized Queries with the SqlDataSource.

始于初秋 2024-10-29 14:21:17
 <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 的文本框检索的

 <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 is the stored proc name and Host is parameter name, which is retreived from a texbox called InputTextBox

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文