.net 中如何使用存儲過程output和return值

數據庫中使用output值和return值:

-------------------------------------------------------

--數獲存儲過程中的OUTPUT參數和返回值獲取

-------------------------------------------------------

 

CREATE PROCEDURE Proc_Test;1

    @INPUT int,

    @OUTPUT int output

AS

BEGIN

    SET NOCOUNT ON;

   

    SELECT @OUTPUT=@INPUT

    RETURN @INPUT+1

   

END

GO

 

--調用output值和return返回值

DECLARE @OUT int,@RETURN int

EXEC @RETURN=Proc_Test;1

       0,

       @OUT output

      

SELECT [返回值]=@RETURN,[OUTPUT]=@OUT

 

返回值        OUTPUT

----------- -----------

1           0

 

 

-----------------------------------------------------

-- SP_EXECUTESQL中的OUTPUT參數獲取

-----------------------------------------------------

DECLARE @Para1 int,@Para2 int,@SUM int

 

EXECUTE SP_EXECUTESQL

N'SELECT @SUM=@Para1+@Para2 ',

N'@Para1 INT,@Para2 INT,@SUM INT OUTPUT ',

5,5,@SUM OUTPUT

 

SELECT [OUTPUT]=@SUM

 

OUTPUT

-----------

10

======================================================================

下面在.net下調用存儲過程:

  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>   
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">   
  6. <head runat="server">   
  7.     <title>使用存儲過程</title>   
  8.     <mce:style type="text/css"><!--  
  9.         #form1   
  10.         {   
  11.             margin-left: 206px;   
  12.         }   
  13.        
  14. --></mce:style><style type="text/css" mce_bogus="1">        #form1   
  15.         {   
  16.             margin-left: 206px;   
  17.         }   
  18.     </style>   
  19. </head>   
  20. <body>   
  21.     <form id="form1" runat="server" style="border-style: none; width: 339px;">   
  22.     <div>   
  23.        
  24.     </div>   
  25.     <asp:Label ID="Label3" runat="server" Text=" 輸  入  參  數:"></asp:Label>   
  26.     <asp:TextBox ID="Input" runat="server" BorderStyle="NotSet"></asp:TextBox>   
  27.  <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/提交.GIF"    
  28.         οnclick="ImageButton1_Click" style="height: 20px" />   
  29.     <hr width="95%" />   
  30.     <br />   
  31.     <asp:Label ID="Label1" runat="server" Text="OUTPUT參數:"></asp:Label>   
  32.     <asp:Label ID="Output" runat="server" BorderColor="#6600FF" BorderStyle="None"    
  33.         BorderWidth="1px" Width="100px">暫無</asp:Label>   
  34.     <hr width="95%" />   
  35.     <br />   
  36.     <asp:Label ID="Label4" runat="server" Text="RETURN返回:"></asp:Label>   
  37.     <asp:Label ID="Return" runat="server" BorderColor="#6600FF" BorderWidth="1px"    
  38.         Width="100px" BorderStyle="None">暫無</asp:Label>   
  39.     </form>   
  40. </body>   
  41. </html>  

 

  1. protected void ImageButton1_Click(object sender, ImageClickEventArgs e)   
  2. {   
  3.     //定義數據庫連接和SqlCommand對象   
  4.     SqlConnection Conn=new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnection"].ToString());   
  5.     SqlCommand Cmd=new SqlCommand("Proc_Test;1",Conn);   
  6.     Cmd.CommandType = CommandType.StoredProcedure;   
  7.        
  8.     //指定參數類型   
  9.     SqlParameter input = Cmd.Parameters.Add("@INPUT", SqlDbType.Int);   
  10.     SqlParameter output = Cmd.Parameters.Add("@OUTPUT", SqlDbType.Int);   
  11.     SqlParameter return_ = Cmd.Parameters.Add("@RETURN", SqlDbType.Int);   
  12.        
  13.     //指定參數方向   
  14.     input.Direction = ParameterDirection.Input;   
  15.     output.Direction = ParameterDirection.Output;   
  16.     return_.Direction = ParameterDirection.ReturnValue;   
  17.   
  18.     //參數賦值   
  19.     if (Input.Text == "")   
  20.     {   
  21.         input.Value = 0;   
  22.     }   
  23.     else  
  24.     {   
  25.         input.Value = Convert.ToInt32(Input.Text);   
  26.     }   
  27.   
  28.     //調用存儲過程   
  29.     Conn.Open();   
  30.     Cmd.ExecuteNonQuery();   
  31.     Conn.Close();   
  32.   
  33.        
  34.     Output.Text = output.Value.ToString();//獲取output值   
  35.     Return.Text = return_.Value.ToString();//獲取返回值   
  36.   
  37. }  

轉自:http://www.cnblogs.com/lengbingshy/archive/2010/02/22/1671262.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章