Use a SqlCommand object to execute a stored procedure

Source: Internet
Author: User

Table tbClass storage Forum category

Field name

Field Type

Null allowed

Description

ClassID

Uniqueidentifier

No

GUID primary key, default value newid ()

ClassName

Varchar (50)

No

Category name

Table tbBoard stores Forum sections in Forum categories

Field name

Field Type

Null allowed

Description

BoardID

Uniqueidentifier

No

GUID primary key, default value newid ()

BoardName

Varchar (50)

No

Forum name

BoardClassID

Uniqueidentifier

No

ID of the category to which the Forum belongs

Stored Procedure spCreateBoard:

Create procedure spCreateBoard

(

@ ClassName varchar (50), // enter the Parameter

@ BoardName varchar (50), // enter the Parameter

@ ClassID varchar (50) output // output parameters

)

AS

Declare @ BoardCount int;

Set @ ClassID = (select ClassID from tbClass where ClassName = @ ClassName );

Insert into tbBoard (BoardName, BoardClassID) values (@ BoardName, @ ClassID );

Set @ BoardCount = (select count (*) from tbBoard );

Return @ BoardCount;

GO

TextBox1 and TextBox2:

Button Event code:

Code
String strCon = "server =.; database = Forum; uid = sa; pwd = OK ";
Using (SqlConnection con = new SqlConnection (strCon ))
{
Con. Open ();
Using (SqlCommand cmd = new SqlCommand ("spCreateBoard", con ))
{
Cmd. CommandType = CommandType. StoredProcedure;
Cmd. Parameters. Add ("@ ClassName", SqlDbType. VarChar, 50 );
Cmd. Parameters ["@ ClassName"]. Value = TextBox1.Text;
// It indicates that it is an input parameter
Cmd. Parameters ["@ ClassName"]. Direction = ParameterDirection. Input;

Cmd. Parameters. Add ("@ BoardName", SqlDbType. VarChar, 50 );
Cmd. Parameters ["@ BoardName"]. Value = TextBox2.Text;
// It indicates that it is an input parameter
Cmd. Parameters ["@ BoardName"]. Direction = ParameterDirection. Input;

Cmd. Parameters. Add ("@ ClassID", SqlDbType. VarChar, 50 );
// Indicates that it is an output parameter.
Cmd. Parameters ["@ ClassID"]. Direction = ParameterDirection. Output;

Cmd. Parameters. Add ("@ BoardCount", SqlDbType. Int );
// Indicates that it is a user-defined function.
Cmd. Parameters ["@ BoardCount"]. Direction = ParameterDirection. ReturnValue;

Cmd. ExecuteNonQuery ();
Foreach (SqlParameter paramter in cmd. Parameters)
{
Response. write (string. format ("parameter {0}, parameter direction {1}, parameter value {2} <br>", paramter. parameterName, paramter. direction, paramter. value ));
}
}
}

Result:

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.