IBatis.Net学习笔记十三:在IBatis.Net中调用存储过程

调用方式比较简单,主要也就是两种类型的存储过程:
1、更新类型的存储过程
2、查询类型的存储过程

下面就来看看具体的调用方式:
1、更新类型的存储过程
sp_insertaccount:


create procedure [dbo].[sp_insertaccount]
    -- add the parameters for the stored procedure here
   @account_id int,
   @account_firstname varchar(32),
   @account_lastname varchar(32)as
begin
insert into accounts (account_id, account_firstname, account_lastname)
    values (@account_id,@account_firstname,@account_lastname )
endmap配置文件:
        <procedure id="insertaccountviastoreprocedure" parametermap="insert-params_new">
            sp_insertaccount
        </procedure>

    <parametermap id="insert-params_new" class="account">
      <parameter property="id" />
      <parameter property="firstname" />
      <parameter property="lastname" />
    </parametermap>

这里要注意的就是parametermap中的参数个数和顺序要和sp_insertaccount存储过程中的一致

ado中的调用代码:

        public void insertaccountviastoreprocedure(account account)
        {
            try
            {
                sqlmap.insert("insertaccountviastoreprocedure", account);
            }
            catch (dataaccessexception ex)
            {
                throw new dataaccessexception("error executing insertaccountviastoreprocedure. cause :" + ex.message, ex);
            }
        }
这里使用的是sqlmap.insert的方法,为了看起来直观一点,其实使用sqlmap.queryforobject方法的话效果也是一样的:)

2、查询类型的存储过程
getaccountbyname:

create procedure [dbo].[getaccountbyname]
    @name varchar(32)
as
begin
select * from accounts where account_firstname like '%' + @name + '%'
end
map配置文件:
    <procedure id="getaccountbynameviastoreprocedure" resultmap="account-result" parametermap="selectpro-params">
      getaccountbyname
    </procedure>

    <parametermap id="selectpro-params" class="string">
      <parameter property="name"/>
    </parametermap>这里parametermap也是和上面的要求一样,至于property的名字在这里没有实际作用,可以任意取名的

ado中的调用代码:
        public arraylist getaccountbynameviastoreprocedure(string strname)
        {
            try
            {
                arraylist list = (arraylist)sqlmap.queryforlist("getaccountbynameviastoreprocedure", strname);
                return list;
            }
            catch (dataaccessexception ex)
            {
                throw new dataaccessexception("error executing sqlaccountviasqlmapdao.getaccountbyid. cause :" + ex.message, ex);
            }
        }

原文地址: http://www.cnblogs.com/firstyi/archive/2008/01/25/1053208.html

转载于:https://www.cnblogs.com/zyfking/archive/2009/01/19/1378527.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值