繁体   English   中英

使用sqldatasource执行存储过程并在vb.net中获取返回值

[英]execute stored procedure using sqldatasource and get return value in vb.net

如何使用sqldatasource执行存储过程并在vb.net中获取返回值。

谢谢,

特里

您正在寻找的方法是DataBind 使用mySqlDataSource.DataBind()调用它

<asp:SqlDataSource 
  ID="sds2" 
  runat="server" 
  ConnectionString="..."
  SelectCommand="spTest"      
  SelectCommandType="StoredProcedure"
  >
  <SelectParameters>
    <asp:ControlParameter ControlID="TextBox1" PropertyName="Text"
                              Name="ParamName" Type="Int32" DefaultValue="0" />
  </SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gv" runat="server" DataSourceID="sds2"></asp:GridView>

当您调用DataBind时,将执行存储过程。 如果GridView控件的DataSourceID属性引用有效的数据源控件,则将自动调用DataBind方法。

 <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中获取的

您需要将SqlConnectionSqlCommand ,如下所示:

using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand("StoredProcedureName", connection)) {
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("SomeParam", someValue);

    object result = command.ExecuteScalar();
}

如果您已经有SP返回值,则必须在数据源的相应事件中获取值。 AKA-已插入,已选择等

这里有几个链接说明了这一点。

http://fredrik.nsquared2.com/viewpost.aspx?PostID=162

http://www.velocityreviews.com/forums/t86158-re-how-to-retrieve-an-output-parameter-using-sqldatasource-control.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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