C# 单击按钮时在 SqlDataSource 中运行存储过程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12966722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running a stored procedure in a SqlDataSource on button click
提问by Gapton
I am building a C# ASP.NET page where I want to call a stored procedure in a database.
我正在构建一个 C# ASP.NET 页面,我想在其中调用数据库中的存储过程。
I have setup a SqlDataSourcewhich points to the stored procedure. Parameters are obtained from the Web's control directly (TextBox)
我已经设置了一个SqlDataSource指向存储过程的。参数直接从Web的控件中获取(TextBox)
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()
or
或者
mySqlDataSource.exec()
or
或者
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?
如何初始化存储过程?以及如何获取存储过程返回的值?
Thank you!
谢谢!
采纳答案by nunespascal
I think the method you are looking for is DataBind. Call it using mySqlDataSource.DataBind()
我认为您正在寻找的方法是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. The DataBind method is called automatically if the DataSourceID property of the GridView control refers to a valid data source control.
调用时执行存储过程DataBind。如果 GridView 控件的 DataSourceID 属性引用有效的数据源控件,则自动调用 DataBind 方法。
回答by ARUNRAJ
//HTML:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
onselecting="SqlDataSource1_Selecting" SelectCommand="testProc_sp"
SelectCommandType="StoredProcedure"></asp:SqlDataSource>
<br />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Button" />
//c#:
protected void Button3_Click(object sender, EventArgs e)
{
SqlDataSource1.DataBind();
}
回答by mortdale
To init a SP from sqldatasource is easy:
从 sqldatasource 初始化一个 SP 很容易:
mySqlDataSource.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
mySqlDataSource.UpdateCommand = "sp_name";
mySqlDataSource.Update();
To get a return value from your SP, I would suggest to use SqlCommand:
要从您的 SP 获取返回值,我建议使用 SqlCommand:
http://forums.asp.net/t/1177022.aspx
http://forums.asp.net/t/1177022.aspx
make sure you have a parameter created with ParameterDirection set to ReturnValue or Output
确保您创建了一个参数,将 ParameterDirection 设置为 ReturnValue 或 Output

