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: