2 回答
TA贡献2036条经验 获得超8个赞
1.直接写个函数传参数
2.用存储过程(这里我就不给你加事务处理啦)
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- 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) output
AS
BEGIN
SET 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)
begin
return 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]=@RoleId
set @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
begin
set @yxshuju='3';
return 3;
end
end
end
END
SET 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;
}
TA贡献2011条经验 获得超2个赞
先说引号
上面这段代码对数据库进行了操作,那么就涉及到sql语句。C#操作数据库时的sql语句是一段传过去的字符串例如:select * from table1
你在查询分析器里可以直接敲这行语句,但是从c#中传过去的就是由引号括起来的字符串了也就是 string sql = "select * from table1";
那么我们如果想把C#里的一个变量a作为条件传到sql中就要这么写:
string a="table1";
string sql="select * from" + a; 这里a是变量,所以字符串拼接后的sql变量就为"select * from table1"了 同样的道理,在C#中逗号,空字符串""就要这么写才能和sql拼接起来
- 2 回答
- 0 关注
- 89 浏览
添加回答
举报