博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net中使用存储过程output值和返回值
阅读量:6857 次
发布时间:2019-06-26

本文共 4788 字,大约阅读时间需要 15 分钟。

数据库中使用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下调用存储过程:

 

<%@ Page Language=
"
C#
" AutoEventWireup=
"
true
"  CodeFile=
"
Default.aspx.cs
" Inherits=
"
_Default
" %>  
  
<!DOCTYPE html PUBLIC 
"
-//W3C//DTD XHTML 1.0 Transitional//EN
" 
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
">  
  
<html xmlns=
"
http://www.w3.org/1999/xhtml
">  
<head runat=
"
server
">  
    <title>使用存储过程</title>  
    <mce:style type=
"
text/css
"><!--  
        #form1   
        {  
            margin-left: 206px;  
        }  
      
-->
</mce:style><style type=
"
text/css
" mce_bogus=
"
1
">
        #form1  
        {  
            margin-left: 206px;  
        }  
    </style>  
</head>  
<body>  
    <form id=
"
form1
" runat=
"
server
" style=
"
border-style: none; width: 339px;
">  
    <div>  
      
    </div>  
    <asp:Label ID=
"
Label3
" runat=
"
server
" Text=
"
 输  入  参  数:
"></asp:Label>  
    <asp:TextBox ID=
"
Input
" runat=
"
server
" BorderStyle=
"
NotSet
"></asp:TextBox>  
 <asp:ImageButton ID=
"
ImageButton1
" runat=
"
server
" ImageUrl=
"
~/提交.GIF
"   
        οnclick=
"
ImageButton1_Click
" style=
"
height: 20px
" />  
    <hr width=
"
95%
" />  
    <br />  
    <asp:Label ID=
"
Label1
" runat=
"
server
" Text=
"
OUTPUT参数:
"></asp:Label>  
    <asp:Label ID=
"
Output
" runat=
"
server
" BorderColor=
"
#6600FF
" BorderStyle=
"
None
"   
        BorderWidth=
"
1px
" Width=
"
100px
">暂无</asp:Label>  
    <hr width=
"
95%
" />  
    <br />  
    <asp:Label ID=
"
Label4
" runat=
"
server
" Text=
"
RETURN返回:
"></asp:Label>  
    <asp:Label ID=
"
Return
" runat=
"
server
" BorderColor=
"
#6600FF
" BorderWidth=
"
1px
"   
        Width=
"
100px
" BorderStyle=
"
None
">暂无</asp:Label>  
    </form>  
</body>  
</html>  
<%@ Page Language=
"
C#
" AutoEventWireup=
"
true
"  CodeFile=
"
Default.aspx.cs
" Inherits=
"
_Default
" %>
<!DOCTYPE html PUBLIC 
"
-//W3C//DTD XHTML 1.0 Transitional//EN
" 
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
">
<html xmlns=
"
http://www.w3.org/1999/xhtml
">
<head runat=
"
server
">
    <title>使用存储过程</title>
    <mce:style type=
"
text/css
"><!--
        #form1
        {
            margin-left: 206px;
        }
    
-->
</mce:style>
<style type=
"
text/css
" mce_bogus=
"
1
">        
    #form1
        {
            margin-left: 206px;
        }
    </style>
</head>
<body>
    <form id=
"
form1
" runat=
"
server
" style=
"
border-style: none; width: 339px;
">
    <div>
    
    </div>
    <asp:Label ID=
"
Label3
" runat=
"
server
" Text=
"
 输  入  参  数:
"></asp:Label>
    <asp:TextBox ID=
"
Input
" runat=
"
server
" BorderStyle=
"
NotSet
"></asp:TextBox>
 <asp:ImageButton ID=
"
ImageButton1
" runat=
"
server
" ImageUrl=
"
~/提交.GIF
" 
        οnclick=
"
ImageButton1_Click
" style=
"
height: 20px
" />
    <hr width=
"
95%
" />
    <br />
    <asp:Label ID=
"
Label1
" runat=
"
server
" Text=
"
OUTPUT参数:
"></asp:Label>
    <asp:Label ID=
"
Output
" runat=
"
server
" BorderColor=
"
#6600FF
" BorderStyle=
"
None
" 
        BorderWidth=
"
1px
" Width=
"
100px
">暂无</asp:Label>
    <hr width=
"
95%
" />
    <br />
    <asp:Label ID=
"
Label4
" runat=
"
server
" Text=
"
RETURN返回:
"></asp:Label>
    <asp:Label ID=
"
Return
" runat=
"
server
" BorderColor=
"
#6600FF
" BorderWidth=
"
1px
" 
        Width=
"
100px
" BorderStyle=
"
None
">暂无</asp:Label>
    </form>
</body>
</html>
 
protected 
void ImageButton1_Click(
object sender, ImageClickEventArgs e)  
{  
    
//
定义数据库连接和SqlCommand对象   
    SqlConnection Conn=
new SqlConnection(ConfigurationManager.ConnectionStrings[
"
TestConnection
"].ToString());  
    SqlCommand Cmd=
new SqlCommand(
"
Proc_Test;1
",Conn);  
    Cmd.CommandType = CommandType.StoredProcedure;  
      
    
//
指定参数类型   
    SqlParameter input = Cmd.Parameters.Add(
"
@INPUT
", SqlDbType.Int);  
    SqlParameter output = Cmd.Parameters.Add(
"
@OUTPUT
", SqlDbType.Int);  
    SqlParameter return_ = Cmd.Parameters.Add(
"
@RETURN
", SqlDbType.Int);  
      
    
//
指定参数方向   
    input.Direction = ParameterDirection.Input;  
    output.Direction = ParameterDirection.Output;  
    return_.Direction = ParameterDirection.ReturnValue;  
  
    
//
参数赋值   
    
if (Input.Text == 
"")  
    {  
        input.Value = 
0;  
    }  
    
else  
    {  
        input.Value = Convert.ToInt32(Input.Text);  
    }  
  
    
//
调用存储过程   
    Conn.Open();  
    Cmd.ExecuteNonQuery();  
    Conn.Close();  
  
      
    Output.Text = output.Value.ToString();
//
获取output值   
    Return.Text = return_.Value.ToString();
//
获取返回值   

 

转载于:https://www.cnblogs.com/ailly/archive/2012/03/05/2380050.html

你可能感兴趣的文章
(转) WTF is computer vision?
查看>>
html标签的target属性应用
查看>>
长连接
查看>>
MySQL数据库权限操作指南
查看>>
rabbitmq的web管理界面无法使用guest用户登录
查看>>
HBase的集群搭建(1、3、5节点都适用)
查看>>
将复杂form表单序列化serialize-object.js
查看>>
C语言 · 出现次数最多的数
查看>>
正则获取HTML代码中img的src地址
查看>>
Java 根据当前时间获取明天、当前周的周五、当前月的最后一天
查看>>
3.View绘制分析笔记之onLayout
查看>>
linux语言设置i18n(转)
查看>>
双链表插入 删除详解
查看>>
迄今为止计算机视觉领域超有实力的研究人物主页
查看>>
Java中值类型和引用类型的区别
查看>>
php 页面间传递数据
查看>>
[Node.js] Initialize a LoopBack Node.js Project through the CLI
查看>>
nginx补丁格式说明(CVE-2016-4450为例)
查看>>
C#编程(八十一)---------- 捕获异常
查看>>
Kinect2.0点云数据获取
查看>>