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

posted on 2009-01-19 20:12  arnold zhang  阅读(298)  评论(0编辑  收藏  举报