ASP.net获取存储过程返回值

来源:互联网 发布:手机域名劫持怎么解决 编辑:程序博客网 时间:2024/06/09 02:03

 存储过程在这里:

--用ParameterDirection.ReturnValue; 返回值
 2 -- =============================================
 3 -- Author:        杨峰    
 4 -- Create date: 2009-08-22    16:18
 5 -- Description:    计算表的记录数
 6 -- =============================================
 7 ALTER PROCEDURE [dbo].[CountNumber]
 8 
 9 AS
10 declare @num int
11 
12 select  @num=count(*from news
13 
14 return @num
15 
16 GO
17 
18 
19 --用ParameterDirection.Output; 参数返回值
20 ALTER PROCEDURE [dbo].[CountNumber]
21 @num int output
22 AS
23 select @num=count(*from news
24 return @num
25 GO

代码在这里!

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> 1  #region 计算出数据表的记录数
 2 
 3  /// <summary>
 4 /// 计算出数据表的记录数
 5 /// </summary>
 6 /// <returns>记录数</returns>
 7 public string CountNumber()
 8 {
 9       
10   cmd = new SqlCommand("CountNumber", GetConn());
11   cmd.CommandType = CommandType.StoredProcedure;
12   SqlParameter i= new SqlParameter("@num", SqlDbType.Int);
13   //i.Direction = ParameterDirection.Output;
14    i.Direction = ParameterDirection.ReturnValue;
15    cmd.Parameters.Add(i);
16    cmd.ExecuteNonQuery();
17 
18   //int strReturn = i.Value.ToString(); //返回值--方法1
19   string num = cmd.Parameters["@num"].Value.ToString();
20     //返回值--方法2
21 
22    conn.Close();
23 
24    return num;
25 
26         }
27 
28         #endregion

 

原创粉丝点击