慕桂英3389331
1.直接写个函数传参数2.用存储过程(这里我就不给你加事务处理啦)set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- =============================================-- Author: <Author,,Name>-- Create date: <Create Date,,>-- Description: <Description,,>-- =============================================CREATE PROCEDURE [dbo].[RoleOperate] -- Add the parameters for the stored procedure here @RoleId varchar(64),--角色ID @RoleName varchar(20),--角色名称 @MaxYear int,--年限 @OperateType int,--操作类型1添加2修改3删除 @yxshuju varchar(MAX) outputASBEGINSET NOCOUNT ON; declare @vroleid varchar(50) declare @vrolename varchar(20) --版本 declare @vmaxyear int --MAC SELECT @vroleid=isnull([RoleId],'') FROM [dbo].[Sys_Role] WHERE [RoleId] = @RoleId if @vroleid<>'' begin if (@OperateType=1) beginreturn 0; end else begin if (@OperateType=2) begin update [dbo].[Sys_Role] set RoleId=@RoleId,RoleName=@RoleName,MaxYear=@MaxYear where [RoleId]=@RoleId set @yxshuju='2'; end else begin delete from [dbo].[Sys_Role] where [RoleId]=@RoleIdset @yxshuju='3'; return 3; end end end else begin if (@OperateType=1) begin insert into[dbo].[Sys_Role](RoleId,RoleName,MaxYear)values(@RoleId,@RoleName,@MaxYear)set @yxshuju='1'; return 1; end else begin if (@OperateType=2) begin set @yxshuju='2'; end else beginset @yxshuju='3'; return 3; end end end ENDSET ANSI_NULLS ON 调用函数: public static int RoleOperate(string RoleId, string RoleName, Int32 MaxYear, Int32 OperateType) { Int32 rValue = 0; try { SQLHelper.ModelSql.Command.CommandText = "RoleOperate"; SqlHelper.SQLHelper.ModelSql.Command.CommandType = CommandType.StoredProcedure; SqlHelper.SQLHelper.ModelSql.Command.Parameters.Clear(); SqlParameter roleid = SQLHelper.ModelSql.Command.Parameters.Add("@RoleId", SqlDbType.VarChar, 50); roleid.Value = RoleId; SqlParameter rolename = SQLHelper.ModelSql.Command.Parameters.Add("@RoleName", SqlDbType.VarChar, 50); rolename.Value = RoleName; SqlParameter maxyear = SQLHelper.ModelSql.Command.Parameters.Add("@MaxYear", SqlDbType.Int); maxyear.Value = MaxYear; SqlParameter operatetype = SQLHelper.ModelSql.Command.Parameters.Add("@OperateType", SqlDbType.Int); operatetype.Value = OperateType; SqlParameter yxshuju = SQLHelper.ModelSql.Command.Parameters.Add("@yxshuju", SqlDbType.VarChar, 300); yxshuju.Direction = ParameterDirection.Output; SQLHelper.ModelSql.Connection.Open(); SQLHelper.ModelSql.Command.ExecuteNonQuery(); rValue = int.Parse(yxshuju.Value.ToString()); } catch (Exception ex) { rValue = -1; } finally { SQLHelper.ModelSql.Connection.Close(); } return rValue; }