简体   繁体   English

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

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

How can I execute a stored procedure using sqldatasource and get the return value in vb.net. 如何使用sqldatasource执行存储过程并在vb.net中获取返回值。

Thanks, 谢谢,

Terri 特里

The method you are looking for is DataBind . 您正在寻找的方法是DataBind Call it using mySqlDataSource.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>

The stored procedure is executed when you call DataBind . 当您调用DataBind时,将执行存储过程。 The DataBind method is called automatically if the DataSourceID property of the GridView control refers to a valid data source control. 如果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 is the stored proc name and Host is parameter name, which is retreived from a texbox called InputTextBox GetProfile是存储的过程名称,Host是参数名称,该名称是从称为InputTextBox的texbox中获取的

You need to use a SqlConnection with a SqlCommand , like this: 您需要将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();
}

If you already have the SP returning a value then you have to grab the value in the corresponding event for the data source. 如果您已经有SP返回值,则必须在数据源的相应事件中获取值。 AKA - Inserted, Selected, etc... AKA-已插入,已选择等

Here's a couple links illustrating the point. 这里有几个链接说明了这一点。

http://fredrik.nsquared2.com/viewpost.aspx?PostID=162 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 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