Stored Procedure with output parameters

Source: Internet
Author: User

Use [northwind]
Go
/***** Object: storedprocedure [DBO]. [users_insert] script Date: 10/19/2009 22:26:16 ******/
Set ansi_nulls off
Go
Set quoted_identifier on
Go
Create procedure [DBO]. [users_insert]
(

@ Uuserid int output,

@ Uusername nvarchar (30 ),

@ Upassword nvarchar (50)
)
As
Insert into [DBO]. [users]
(
[U_username]
, [U_password]
)
Values
(
@ Uusername
, @ Upassword
)

-- Get the identity value
Set @ uuserid = scope_identity ()

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

Declare @ ID int;
Exec users_insert @ ID output, 'aaa', 'aaa'
Select
@ ID;

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

 

Using (sqlcommand cmd = new sqlcommand ("proc_userinfo", con ))
14 {
15 // What is the function? Remember to call the stored procedure. This statement cannot be fewer.
16 cmd. commandtype = commandtype. storedprocedure;
17 // Add the input/output parameter type and input parameter value of the stored procedure
18 cmd. Parameters. Add ("@ username", sqldbtype. varchar, 50). value = username;
19 cmd. Parameters. Add ("@ PWD", sqldbtype. varchar, 50). value = PWD;
20 cmd. Parameters. Add ("@ Grade", sqldbtype. varchar, 50). value = Grade;
21 cmd. Parameters. Add ("@ count", sqldbtype. INT );
22 cmd. Parameters ["@ username"]. dbtype = dbtype. String;
23 // specify whether the parameter type is input or output
24 cmd. Parameters ["@ username"]. Direction = parameterdirection. input;
25 cmd. Parameters ["@ PWD"]. dbtype = dbtype. String;
26 // specify whether the parameter type is input or output
27 cmd. Parameters ["@ PWD"]. Direction = parameterdirection. input;
28 cmd. Parameters ["@ Grade"]. dbtype = dbtype. String;
29 // specify whether the parameter type is input or output
30 cmd. Parameters ["@ Grade"]. Direction = parameterdirection. input;
31 cmd. Parameters ["@ count"]. dbtype = dbtype. int32;
32 // specify whether the parameter type is input or output
33 cmd. Parameters ["@ count"]. Direction = parameterdirection. output;
34 // The stored procedure is similar to a query statement.
35 cmd. executescalar ();
36 // accept the returned values after the stored procedure is executed
37 n = (INT) cmd. Parameters ["@ count"]. value;

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.