第 3 课:检索报表定义的字段列表
由于每个报表定义都应该具有字段(表示报表数据)列表,因此,您必须从查询生成字段列表。
生成字段列表
- 请用以下代码替换您项目中 GenerateFieldsList() 方法的代码:
Public Sub GenerateFieldsList()
Dim command As SqlCommand
Dim reader As SqlDataReader
' Executing a query to retrieve a fields list for the report
command = m_connection.CreateCommand()
m_commandText = "SELECT Person.CountryRegion.Name AS CountryName, Person.StateProvince.Name AS StateProvince " + "FROM Person.StateProvince " + "INNER JOIN Person.CountryRegion ON Person.StateProvince.CountryRegionCode = Person.CountryRegion.CountryRegionCode " + "ORDER BY Person.CountryRegion.Name"
command.CommandText = m_commandText
' Execute and create a reader for the current command
reader = command.ExecuteReader(CommandBehavior.SchemaOnly)
' For each field in the resultset, add the name to an array list
m_fields = New ArrayList()
Dim i As Integer
For i = 0 To reader.FieldCount - 1
m_fields.Add(reader.GetName(i))
Next i
End Sub 'GenerateFieldsList
public void GenerateFieldsList()
{
SqlCommand command;
SqlDataReader reader;
// Executing a query to retrieve a fields list for the report
command = m_connection.CreateCommand();
m_commandText =
"SELECT Person.CountryRegion.Name AS CountryName, Person.StateProvince.Name AS StateProvince " +
"FROM Person.StateProvince " +
"INNER JOIN Person.CountryRegion ON Person.StateProvince.CountryRegionCode = Person.CountryRegion.CountryRegionCode " +
"ORDER BY Person.CountryRegion.Name";
command.CommandText = m_commandText;
// Execute and create a reader for the current command
reader = command.ExecuteReader(CommandBehavior.SchemaOnly);
// For each field in the resultset, add the name to an array list
m_fields = new ArrayList();
for (int i = 0; i <= reader.FieldCount - 1; i++)
{
m_fields.Add(reader.GetName(i));
}
}
请参阅
任务
第 4 课:创建生成报表定义文件的代码
教程:使用 .NET Framework 生成 RDL