Three values stored procedure to give

But the fate of the best in ASP.NET and SQL SERVER, slightly larger procedures are generally the first to consider is that SQL SERVER, just some very economical considerations before using ACCESS wait. Using SQL SERVER, in order to make a better efficiency of the database, usually only get stored procedures, stored procedures because fast execution speed, and can achieve some advanced query capabilities. Some data such as incoming parameters, but the execution of SQL procedures, etc. may be different.
Here's an example, the establishment of a new role requires the role name can not be repeated, the following is a stored procedure.   
CREATE PROCEDURE sp_AccountRole_Create

@CategoryID int,
@RoleName nvarchar(10),
@Description nvarchar(50),
@RoleID int output
AS
DECLARE @Count int

- find out if records have the same name as
the SELECT @count = the Count (the RoleID) the FROM Account_Role the WHERE
RoleName = @rolename

IF @Count = 0

INSERT INTO Account_Role
(CategoryID, RoleName, Description) valueS
(@CategoryID, @RoleName, @Description)

SET @RoleID = @@IDENTITY

. 1 the RETURN
the GO

  
  execute a stored procedure C # procedure:
the SqlConnection the DbConnection the SqlConnection new new = (mConnectionString);
SqlCommand Command = new new SqlCommand ( "sp_AccountRole_Create", the DbConnection);
DbConnection.Open (connectString);
// attribute SqlCommand abandoned stored procedure
command. CommandType = CommandType.StoredProcedure;

 
  

command.Parameters.Add("@CategoryID", SqlDbType.Int, 4);
command.Parameters.Add("@RoleName", SqlDbType.NVarChar, 10);
command.Parameters.Add("@Description", SqlDbType.NVarChar, 50);
command.Parameters.Add("@RoleID", SqlDbType.Int, 4);
// 返回值
command.Parameters.Add("Returnvalue",
SqlDbType.Int,
4, // Size
ParameterDirection.Returnvalue,
false, // is nullable
0, // byte precision
0, // byte scale
string.Empty,
DataRowVersion.Default,
null );

command.parameters["@CategoryID"].value = permission.CategoryID;
command.parameters["@RoleName"].value = permission.PermissionName;
command.parameters["@Description"].value = permission.Description;
// 可以返回新的ID值
command.parameters["@RoleID"].Direction = ParameterDirection.Output;

int rowsAffected = command.ExecuteNonQuery ();
Here's an example, establishing a new role, the role of the required name can not be repeated, the following is a stored procedure.   

CREATE PROCEDURE sp_AccountRole_Create

@CategoryID int,
@RoleName nvarchar(10),
@Description nvarchar(50),
@RoleID int output
AS
DECLARE @Count int

- find out if records have the same name as
the SELECT @count = the Count (the RoleID) the FROM Account_Role the WHERE
RoleName = @rolename

IF @Count = 0

INSERT INTO Account_Role
(CategoryID, RoleName, Description) valueS
(@CategoryID, @RoleName, @Description)

SET @RoleID = @@IDENTITY

RETURN 1
GO


  
  C # stored procedure execution process:

  

SqlConnection DbConnection = new SqlConnection(mConnectionString);
SqlCommand command = new SqlCommand( "sp_AccountRole_Create", DbConnection );
DbConnection.Open(connectString);
// 废置SqlCommand的属性为存储过程
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add("@CategoryID", SqlDbType.Int, 4);
command.Parameters.Add("@RoleName", SqlDbType.NVarChar, 10);
command.Parameters.Add("@Description", SqlDbType.NVarChar, 50);
command.Parameters.Add("@RoleID", SqlDbType.Int, 4);
// 返回值
command.Parameters.Add("Returnvalue",
SqlDbType.Int,
4, // Size
ParameterDirection.Returnvalue,
false, // is nullable
0, // byte precision
0, // byte scale
string.Empty,
DataRowVersion.Default,
null );

command.parameters["@CategoryID"].value = permission.CategoryID;
command.parameters["@RoleName"].value = permission.PermissionName;
command.parameters["@Description"].value = permission.Description;
// 可以返回新的ID值
command.parameters["@RoleID"].Direction = ParameterDirection.Output;

int rowsAffected = command.ExecuteNonQuery();
int result = command.parameters["Returnvalue"].value;
int newID = command.parameters["@RoleID"].value;

 
  

 Very strong function of it, you can get three values, namely lines affect the values, the stored procedure returns a value, the new ID value.



Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2007/05/06/736733.html

Guess you like

Origin blog.csdn.net/weixin_34402090/article/details/93495923
Recommended