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配置文件:-- 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
<procedure id&#61;"InsertAccountViaStoreProcedure" parameterMap&#61;"insert-params_new">
sp_InsertAccount
procedure>
<parameterMap id&#61;"insert-params_new" class&#61;"Account">
<parameter property&#61;"Id" />
<parameter property&#61;"FirstName" />
<parameter property&#61;"LastName" />
parameterMap>
sp_InsertAccount
procedure>
<parameterMap id&#61;"insert-params_new" class&#61;"Account">
<parameter property&#61;"Id" />
<parameter property&#61;"FirstName" />
<parameter property&#61;"LastName" />
parameterMap>
这里要注意的就是ParameterMap中的参数个数和顺序要和sp_InsertAccount存储过程中的一致
Ado中的调用代码&#xff1a;
public void InsertAccountViaStoreProcedure(Account account)
{
try
{
sqlMap.Insert("InsertAccountViaStoreProcedure", account);
}
catch (DataAccessException ex)
{
throw new DataAccessException("Error executing InsertAccountViaStoreProcedure. Cause :" &#43; ex.Message, ex);
}
}
{
try
{
sqlMap.Insert("InsertAccountViaStoreProcedure", account);
}
catch (DataAccessException ex)
{
throw new DataAccessException("Error executing InsertAccountViaStoreProcedure. Cause :" &#43; ex.Message, ex);
}
}
这里使用的是sqlMap.Insert的方法&#xff0c;为了看起来直观一点&#xff0c;其实使用sqlMap.QueryForObject方法的话效果也是一样的:)
2、查询类型的存储过程
GetAccountByName&#xff1a;
CREATE PROCEDURE [dbo].[GetAccountByName]
&#64;name varchar(32)
AS
BEGIN
select * from accounts where Account_FirstName like &#39;%&#39; &#43; &#64;name &#43; &#39;%&#39;
END
&#64;name varchar(32)
AS
BEGIN
select * from accounts where Account_FirstName like &#39;%&#39; &#43; &#64;name &#43; &#39;%&#39;
END
Map配置文件&#xff1a;
<procedure id&#61;"GetAccountByNameViaStoreProcedure" resultMap&#61;"account-result" parameterMap&#61;"selectpro-params">
GetAccountByName
procedure>
<parameterMap id&#61;"selectpro-params" class&#61;"string">
<parameter property&#61;"name"/>
parameterMap>
这里parameterMap也是和上面的要求一样&#xff0c;至于property的名字在这里没有实际作用&#xff0c;可以任意取名的GetAccountByName
procedure>
<parameterMap id&#61;"selectpro-params" class&#61;"string">
<parameter property&#61;"name"/>
parameterMap>
Ado中的调用代码&#xff1a;
public ArrayList GetAccountByNameViaStoreProcedure(string strName)
{
try
{
ArrayList list &#61; (ArrayList)sqlMap.QueryForList("GetAccountByNameViaStoreProcedure", strName);
return list;
}
catch (DataAccessException ex)
{
throw new DataAccessException("Error executing SqlAccountViaSqlMapDao.GetAccountById. Cause :" &#43; ex.Message, ex);
}
}
{
try
{
ArrayList list &#61; (ArrayList)sqlMap.QueryForList("GetAccountByNameViaStoreProcedure", strName);
return list;
}
catch (DataAccessException ex)
{
throw new DataAccessException("Error executing SqlAccountViaSqlMapDao.GetAccountById. Cause :" &#43; ex.Message, ex);
}
}