• 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 )
    END
    Map配置文件:
            <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);
                }

            }
  • 相关阅读:
    【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)
    【BZOJ】2101: [Usaco2010 Dec]Treasure Chest 藏宝箱(dp)
    【BZOJ】3404: [Usaco2009 Open]Cow Digit Game又见数字游戏(博弈论)
    【BZOJ】3403: [Usaco2009 Open]Cow Line 直线上的牛(模拟)
    【BZOJ】3402: [Usaco2009 Open]Hide and Seek 捉迷藏(spfa)
    【BZOJ】3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队(dp)
    【BZOJ】3399: [Usaco2009 Mar]Sand Castle城堡(贪心)
    【BZOJ】3392: [Usaco2005 Feb]Part Acquisition 交易(spfa)
    【BZOJ】2020: [Usaco2010 Jan]Buying Feed, II (dp)
    【BZOJ】2015: [Usaco2010 Feb]Chocolate Giving(spfa)
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/2052802.html
Copyright © 2020-2023  润新知