在orm开发中也会用到存储过程,案例如下:
Create proc [dbo].[proc_GetCompanyStructureByDepartStructureId] @Com_StructureId uniqueidentifier
AS
BEGIN
declare @structureType int
declare @parentStructureId uniqueidentifier
select @structureType=b.Type,@parentStructureId=a.ParentStructureID from Com_Structure a inner join Com_StructureType b
on a.Com_SructureTypeID=b.Com_StructureTypeID where [email protected]_StructureId
if(@structureType=2)--2表示部门
select Top 1 * from Com_Structure where [email protected]
END
GO
sql中直接调用存储过程 exec proc_GetCompanyStructureByDepartStructureId ‘47F10D40-535A-4AF8-AD98-81616A2822B1‘
orm调用存储过程 如下:
Guid CompanyStructureId;
var dr = McDB.DBContext.StoredProcedure("proc_GetCompanyStructureByDepartStructureId").AddInputParameter("@Com_StructureId", System.Data.DbType.Guid, id).ToDataReader();
while (dr.Read())
{
CompanyStructureId = Guid.Parse(dr["Com_StructureId"].ToString()); //存储过程返回一条记录,取其中的单个值
}
dr.Close();