通过BDC访问存储过程及通过对象模型从BDC获取数据
前段时间在项目中需要用到BDC,于是学习了一下。
主要参考《Manning.SharePoint.2007.Developers.Guide.to.Business.Data.Catalog》,该书在网上有PDF版下载。
主要工具用的是bdc meta man4.0.0.3开发人员版。
对于如何通过BDC访问表或视图,书里写的很清楚,做起来也很简单,就不说了。
但是在开发中遇到的两个问题,在书中却没有找到答案:
如何通过BDC访问存储过程?
通过对象模型访问BDC时,如何传入参数?
后来通过搜索和自己的摸索,总算解决了这两个问题,在此记录一下。
这是应用程序定义文件Entity部分:
<Entity EstimatedInstanceCount="0" Name="proc_kechengbiao_by_Unit">
<Methods>
<Method Name="dbo.[proc_kechengbiao_by_Unit]">
<Properties>
<Property Name="RdbCommandText" Type="System.String">proc_kechengbiao_by_Unit</Property>
<Property Name="RdbCommandType" Type="System.Data.CommandType">StoredProcedure</Property>
</Properties>
<FilterDescriptors>
<FilterDescriptor Type="Comparison" Name="ViewMode" />
<FilterDescriptor Type="Comparison" Name="Date" />
<FilterDescriptor Type="Comparison" Name="Language" />
</FilterDescriptors>
<Parameters>
<Parameter Direction="In" Name="@language">
<TypeDescriptor TypeName="System.String" Name="language" AssociatedFilter="Language">
<DefaultValues>
<DefaultValue MethodInstanceName="dbo.[proc_kechengbiao_by_Unit]Finder" Type="System.String">cn</DefaultValue>
</DefaultValues>
</TypeDescriptor>
</Parameter>
<Parameter Direction="In" Name="@today">
<TypeDescriptor TypeName="System.String" Name="today" AssociatedFilter="Date">
<DefaultValues>
<DefaultValue MethodInstanceName="dbo.[proc_kechengbiao_by_Unit]Finder" Type="System.String">2009-12-01</DefaultValue>
</DefaultValues>
</TypeDescriptor>
</Parameter>
<Parameter Direction="In" Name="@unit">
<TypeDescriptor TypeName="System.String" Name="unit" AssociatedFilter="ViewMode">
<DefaultValues>
<DefaultValue MethodInstanceName="dbo.[proc_kechengbiao_by_Unit]Finder" Type="System.String">today</DefaultValue>
</DefaultValues>
</TypeDescriptor>
</Parameter>
<Parameter Direction="Return" Name="dbo.proc_kechengbiao_by_Unit">
<TypeDescriptor TypeName="System.Data.IDataReader, System.Data, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" IsCollection="true" Name="dbo.[proc_kechengbiao_by_Unit]DataReader">
<TypeDescriptors>
<TypeDescriptor TypeName="System.Data.IDataRecord, System.Data, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Name="dbo.[proc_kechengbiao_by_Unit]DataRecord">
<TypeDescriptors>
<TypeDescriptor TypeName="System.String" Name="bcbh" />
<TypeDescriptor TypeName="System.String" Name="ktbh" />
<TypeDescriptor TypeName="System.Int32" Name="pdbh" />
<TypeDescriptor TypeName="System.String" Name="bcmmcn" />
<TypeDescriptor TypeName="System.String" Name="kcmc" />
<TypeDescriptor TypeName="System.String" Name="zjxm" />
<TypeDescriptor TypeName="System.String" Name="today" />
<TypeDescriptor TypeName="System.String" Name="kssj" />
<TypeDescriptor TypeName="System.String" Name="jssj" />
<TypeDescriptor TypeName="System.String" Name="sksj" />
<TypeDescriptor TypeName="System.String" Name="weekday" />
<TypeDescriptor TypeName="System.String" Name="jxdd" />
<TypeDescriptor TypeName="System.Int32" Name="rybh" />
<TypeDescriptor TypeName="System.String" Name="yybh" />
<TypeDescriptor TypeName="System.Int32" Name="jybh" />
<TypeDescriptor TypeName="System.String" Name="yyname" />
<TypeDescriptor TypeName="System.String" Name="ISShow" />
<TypeDescriptor TypeName="System.Int64" Name="opened" />
</TypeDescriptors>
</TypeDescriptor>
</TypeDescriptors>
</TypeDescriptor>
</Parameter>
</Parameters>
<MethodInstances>
<MethodInstance Name="dbo.[proc_kechengbiao_by_Unit]Finder" Type="Finder" ReturnParameterName="dbo.proc_kechengbiao_by_Unit" ReturnTypeDescriptorName="dbo.[proc_kechengbiao_by_Unit]DataReader" ReturnTypeDescriptorLevel="0" />
</MethodInstances>
</Method>
</Methods>
</Entity>
<Methods>
<Method Name="dbo.[proc_kechengbiao_by_Unit]">
<Properties>
<Property Name="RdbCommandText" Type="System.String">proc_kechengbiao_by_Unit</Property>
<Property Name="RdbCommandType" Type="System.Data.CommandType">StoredProcedure</Property>
</Properties>
<FilterDescriptors>
<FilterDescriptor Type="Comparison" Name="ViewMode" />
<FilterDescriptor Type="Comparison" Name="Date" />
<FilterDescriptor Type="Comparison" Name="Language" />
</FilterDescriptors>
<Parameters>
<Parameter Direction="In" Name="@language">
<TypeDescriptor TypeName="System.String" Name="language" AssociatedFilter="Language">
<DefaultValues>
<DefaultValue MethodInstanceName="dbo.[proc_kechengbiao_by_Unit]Finder" Type="System.String">cn</DefaultValue>
</DefaultValues>
</TypeDescriptor>
</Parameter>
<Parameter Direction="In" Name="@today">
<TypeDescriptor TypeName="System.String" Name="today" AssociatedFilter="Date">
<DefaultValues>
<DefaultValue MethodInstanceName="dbo.[proc_kechengbiao_by_Unit]Finder" Type="System.String">2009-12-01</DefaultValue>
</DefaultValues>
</TypeDescriptor>
</Parameter>
<Parameter Direction="In" Name="@unit">
<TypeDescriptor TypeName="System.String" Name="unit" AssociatedFilter="ViewMode">
<DefaultValues>
<DefaultValue MethodInstanceName="dbo.[proc_kechengbiao_by_Unit]Finder" Type="System.String">today</DefaultValue>
</DefaultValues>
</TypeDescriptor>
</Parameter>
<Parameter Direction="Return" Name="dbo.proc_kechengbiao_by_Unit">
<TypeDescriptor TypeName="System.Data.IDataReader, System.Data, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" IsCollection="true" Name="dbo.[proc_kechengbiao_by_Unit]DataReader">
<TypeDescriptors>
<TypeDescriptor TypeName="System.Data.IDataRecord, System.Data, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Name="dbo.[proc_kechengbiao_by_Unit]DataRecord">
<TypeDescriptors>
<TypeDescriptor TypeName="System.String" Name="bcbh" />
<TypeDescriptor TypeName="System.String" Name="ktbh" />
<TypeDescriptor TypeName="System.Int32" Name="pdbh" />
<TypeDescriptor TypeName="System.String" Name="bcmmcn" />
<TypeDescriptor TypeName="System.String" Name="kcmc" />
<TypeDescriptor TypeName="System.String" Name="zjxm" />
<TypeDescriptor TypeName="System.String" Name="today" />
<TypeDescriptor TypeName="System.String" Name="kssj" />
<TypeDescriptor TypeName="System.String" Name="jssj" />
<TypeDescriptor TypeName="System.String" Name="sksj" />
<TypeDescriptor TypeName="System.String" Name="weekday" />
<TypeDescriptor TypeName="System.String" Name="jxdd" />
<TypeDescriptor TypeName="System.Int32" Name="rybh" />
<TypeDescriptor TypeName="System.String" Name="yybh" />
<TypeDescriptor TypeName="System.Int32" Name="jybh" />
<TypeDescriptor TypeName="System.String" Name="yyname" />
<TypeDescriptor TypeName="System.String" Name="ISShow" />
<TypeDescriptor TypeName="System.Int64" Name="opened" />
</TypeDescriptors>
</TypeDescriptor>
</TypeDescriptors>
</TypeDescriptor>
</Parameter>
</Parameters>
<MethodInstances>
<MethodInstance Name="dbo.[proc_kechengbiao_by_Unit]Finder" Type="Finder" ReturnParameterName="dbo.proc_kechengbiao_by_Unit" ReturnTypeDescriptorName="dbo.[proc_kechengbiao_by_Unit]DataReader" ReturnTypeDescriptorLevel="0" />
</MethodInstances>
</Method>
</Methods>
</Entity>
下面是通过对象模型访问BDC的部分:
public static DataTable GetCurriculumSchedule(DateTime date, string viewMode)
{
DataTable rtn = null;
LobSystemInstance lob = ApplicationRegistry.GetLobSystemInstanceByName("CourseSiteInstance");
Entity e = lob.GetEntities()["proc_kechengbiao_by_Unit"];
FilterCollection fc = e.GetFinderFilters();
ComparisonFilter filter_Date = GetComparisonFilterComparisonFilterByName(fc, "Date");
filter_Date.Value = date.ToString("yyyy-MM-dd");
ComparisonFilter filter_ViewMode = GetComparisonFilterComparisonFilterByName(fc, "ViewMode");
filter_ViewMode.Value = viewMode;
IEntityInstanceEnumerator recordEnum = e.FindFiltered(fc, lob);
rtn = BuildDataTable(e.GetFinderView().Fields);
while (recordEnum.MoveNext())
{
IEntityInstance ie = recordEnum.Current;
DataRow dr = rtn.NewRow();
foreach (Field f in e.GetFinderView().Fields)
{
if (ie[f] != null)
{
dr[f.Name] = ie[f];
}
}
rtn.Rows.Add(dr);
}
return rtn;
}
/// <summary>
/// 根据BDC Entity创建DataTable。不包含数据,只创建对应的列。
/// </summary>
/// <param name="fieldCollection"></param>
/// <returns></returns>
public static DataTable BuildDataTable(FieldCollection fieldCollection)
{
DataTable dt = new DataTable();
foreach (Field f in fieldCollection)
{
DataColumn dc = new DataColumn(f.Name,
Type.GetType(f.TypeDescriptor.TypeName));
dt.Columns.Add(dc);
}
return dt;
}
/// <summary>
/// 找出具有指定名称的Filter
/// </summary>
/// <param name="filters"></param>
/// <param name="filterName"></param>
/// <returns></returns>
public static ComparisonFilter GetComparisonFilterComparisonFilterByName(FilterCollection filters, string filterName)
{
foreach (FilterBase f in filters)
{
if (f.Name == filterName)
return f as ComparisonFilter;
}
return null;
}