在按钮点击一个SqlDataSource运行存储过程 [英] Running a stored procedure in a SqlDataSource on button click

查看:154
本文介绍了在按钮点击一个SqlDataSource运行存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设,我想打电话给在数据库中的存储过程的C#ASP.NET页面。

I am building a C# ASP.NET page where I want to call a stored procedure in a database.

我已经建立了的SqlDataSource 指向存储过程。参数是直接从Web的控制得到(文本框)

I have setup a SqlDataSource which points to the stored procedure. Parameters are obtained from the Web's control directly (TextBox)

我有一个按钮,当用户点击它,它应该运行存储过程,所以我要添加code是这样的:

I have a button, when the user clicks it it should run the stored procedure, so I was going to add code like this :

mySqlDataSource.run()

mySqlDataSource.exec()

mySqlDataSource.storedProcedure()

但是,当然这些方法存在

but of course none of these methods exist.

我如何初始存储过程?和我怎么用存储过程返回的值吗?

How do I initial the stored procedure? and how do I get the value returned by the stored procedure please?

感谢您!

推荐答案

我认为你正在寻找的方法是的DataBind 。使用叫它 mySqlDataSource.DataBind()

I think the method you are looking for is DataBind. Call it using 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方法被自动调用。

The stored procedure is executed when you call DataBind. The DataBind method is called automatically if the DataSourceID property of the GridView control refers to a valid data source control.

这篇关于在按钮点击一个SqlDataSource运行存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆