Dapper -- 调用存储过程

只带输入参数存储过程的使用

CREATE PROCEDURE [dbo].[QueryMember] 
@MemberId varchar(50)='',
@Name varchar(50)=''
AS
BEGIN
    SELECT * FROM Member where MemberID=@MemberId and Name=@Name;
END

public void UseStoredProcedure()
{
     var info = Database.Query("QueryMember", new
                                    {
                                       MemberId = "20192225",
                                       Name = "张三"
                                     },
     commandType: CommandType.StoredProcedure);
     
}

带输入输出存储过程的使用

CREATE PROCEDURE [dbo].[QueryMember] 
@MemberId varchar(50)='',
@Name varchar(50)='',
@errorFlag int output, --错误标志
@errorMsg varchar(200) output --错误信息
AS
BEGIN
    SELECT * FROM Member where MemberID=@MemberId and Name=@Name;
    select @errorflag=0,@errormsg='查询成功'
END
public string UseStoredProcedure()
{
       DynamicParameters dp = new DynamicParameters();
       dp.Add("@MemberId", "2019595");
       dp.Add("@Name", "张三");
       dp.Add("@errorMsg", "", DbType.String, ParameterDirection.Output);
       dp.Add("@errorFlag", "", DbType.Int32, ParameterDirection.Output);
       Database.Execute("QueryMember", dp, null, null, CommandType.StoredProcedure);
       string errorMsg = dp.Get("@errorMsg");
       int errorFlag = dp.Get("@errorFlag");
       retrun errorMsg;
}

不带参数的存储过程的使用

CREATE PROCEDURE [dbo].[QueryMember] 
AS
BEGIN
    SELECT * FROM Member 
END
public List UseStoredProcedure()
{
    List MemberList = new List();
    MemberList = Database.Query("QueryMember",
                     null,
                     null,
                     true,
                     null,
                     CommandType.StoredProcedure).ToList();
   return MemberList;
}

你可能感兴趣的:(Back-End)