将外部数据集用于 Reporting Services

DataSet 对象是支持具有 ADO.NET 的断开连接的、分布式数据方案的核心。 DataSet 对象是数据的驻留内存表示形式,提供与数据源无关的一致的关系编程模型。 它可用于具有 XML 数据的多种不同的数据源,或者用于管理应用程序的本地数据。 DataSet 对象表示完整的数据集合,包括相关表、约束以及表之间的关系。 由于 DataSet 对象既存储数据、又公开数据,因此,数据可能会经常被处理并转换为 DataSet 对象,然后发生针对这些数据的报告。

使用 Reporting Services 数据处理扩展插件,您可以集成外部应用程序创建的任何自定义 DataSet 对象。 为此,在 Reporting Services 中创建一个自定义数据处理扩展插件,充当您的 DataSet 对象和报表服务器之间的桥梁。 用于处理此 DataSet 对象的大多数代码包含在您创建的 DataReader 类中。

向报表服务器公开您的 DataSet 对象的第一步是在可填充 DataSet 对象的 DataReader 类中实现特定于访问接口的方法。 以下示例说明如何通过在 DataReader 类中使用特定于访问接口的方法,将静态数据加载到 DataSet 对象中。

'Private members of the DataReader class
Private m_dataSet As System.Data.DataSet
Private m_currentRow As Integer

'Method to create a dataset
Friend Sub CreateDataSet()
   ' Create a dataset.
   Dim ds As New System.Data.DataSet("myDataSet")
   ' Create a data table. 
   Dim dt As New System.Data.DataTable("myTable")
   ' Create a data column and set various properties. 
   Dim dc As New System.Data.DataColumn()
   dc.DataType = System.Type.GetType("System.Decimal")
   dc.AllowDBNull = False
   dc.Caption = "Number"
   dc.ColumnName = "Number"
   dc.DefaultValue = 25
   ' Add the column to the table. 
   dt.Columns.Add(dc)
   ' Add 10 rows and set values. 
   Dim dr As System.Data.DataRow
   Dim i As Integer
   For i = 0 To 9
      dr = dt.NewRow()
      dr("Number") = i + 1
      ' Be sure to add the new row to the DataRowCollection. 
      dt.Rows.Add(dr)
   Next i

   ' Fill the dataset.
   ds.Tables.Add(dt)

   ' Use a private variable to store the dataset in your
   ' DataReader.
   m_dataSet = ds

   ' Set the current row to -1.
   m_currentRow = - 1
End Sub 'CreateDataSet
// Private members of the DataReader class
private System.Data.DataSet m_dataSet;
private int m_currentRow;

// Method to create a dataset
internal void CreateDataSet()
{
   // Create a dataset.
   System.Data.DataSet ds = new System.Data.DataSet("myDataSet");
   // Create a data table. 
   System.Data.DataTable dt = new System.Data.DataTable("myTable");
   // Create a data column and set various properties. 
   System.Data.DataColumn dc = new System.Data.DataColumn(); 
   dc.DataType = System.Type.GetType("System.Decimal"); 
   dc.AllowDBNull = false; 
   dc.Caption = "Number"; 
   dc.ColumnName = "Number"; 
   dc.DefaultValue = 25; 
   // Add the column to the table. 
   dt.Columns.Add(dc); 
   // Add 10 rows and set values. 
   System.Data.DataRow dr; 
   for(int i = 0; i < 10; i++)
   { 
      dr = dt.NewRow(); 
      dr["Number"] = i + 1; 
      // Be sure to add the new row to the DataRowCollection. 
      dt.Rows.Add(dr);
   }

   // Fill the dataset.
   ds.Tables.Add(dt);

   // Use a private variable to store the dataset in your
   // DataReader.
   m_dataSet = ds;

   // Set the current row to -1.
   m_currentRow = -1;
}
public bool Read()
{
   m_currentRow++;
   if (m_currentRow >= m_dataSet.Tables[0].Rows.Count) 
   {
      return (false);
   } 
   else 
   {
      return (true);
   }
}

public int FieldCount
{
   // Return the count of the number of columns, which in
   // this case is the size of the column metadata
   // array.
   get { return m_dataSet.Tables[0].Columns.Count; }
}

public string GetName(int i)
{
   return m_dataSet.Tables[0].Columns[i].ColumnName;
}

public Type GetFieldType(int i)
{
   // Return the actual Type class for the data type.
   return m_dataSet.Tables[0].Columns[i].DataType;
}

public Object GetValue(int i)
{
   return m_dataSet.Tables[0].Rows[m_currentRow][i];
}

public int GetOrdinal(string name)
{
   // Look for the ordinal of the column with the same name and return it.
   // Returns -1 if not found.
   return m_dataSet.Tables[0].Columns[name].Ordinal;
}

一旦创建或检索数据集后,就可以在实现 DataReader 类的 ReadGetValueGetNameGetOrdinalGetFieldTypeFieldCount 成员时使用 DataSet 对象。

请参阅

参考

Reporting Services 扩展插件库

其他资源

Reporting Services 扩展插件

实现数据处理扩展插件