这是我想要实现的一项简单任务,但 ASP.NET 使它变得非常困难,几乎不可能。我关注了这个问题
Running a Stored Procedure in C# Button但发现ExecuteNonQuery
没有返回查询的输出。
我尝试了其他方法,但似乎无法以这种方式传递参数
SqlConnection myConnection = new SqlConnection(myconnectionString);
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.CommandText = "usp_GetCustomer";
myCommand.SelectParameter <-- does not exist
谁能写出这个简单的代码,我该如何实现它?基本上,我将 @username
和 @month
(都是字符串)传递给存储过程,它返回一个我想要捕获并分配给标签控件的数字。
谢谢
我的查询的输出是这样的。它运行一个复杂的查询,创建一个临时表,然后运行
select @@rowcount
我正在捕捉这一点。
最佳答案
不要使用
SqlCommand.<a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx" rel="noreferrer noopener nofollow">ExecuteNonQuery()</a>
如果您确实想要结果集中的数据。确保您的程序使用
set nocount on
然后使用
SqlCommand.<a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx" rel="noreferrer noopener nofollow">ExecuteScalar()</a>
return (int)myCommand.ExecuteScalar(); // value of select @@rowcount
编辑:至于您的参数:
myCommand.Parameters.AddWithValue("@username","jsmith");
myCommand.Parameters.AddWithValue("@month","January");
关于c# - 在 C# 中运行存储过程,传递参数并捕获输出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7760008/