在最近的报表开发中,有这样一张报表,如图:

要求用GridView显示,所以查询的结果要么是一个临时表或者是一个查询视图,想到sql2005中加入CLR的支持,有关CLR的操作请看体验:用C#写存储过程(VS.NET 2005) 或者在sqlserver2005中部署C#编写的自定义函数 ,实现的类如下

1
using System;
2
using System.Collections.Generic;
3
using System.Data;
4
using System.Data.SqlClient;
5
using System.Data.SqlTypes;
6
using Microsoft.SqlServer.Server;
7
using System.Collections;
8
9
public partial class WYTableFunction
10
{
11
#region Fun_FactIncome_CLR
12
//这个特性定义了一个sql表值函数,此函数返回的表的定义为:String nvarchar(200)
13
//并且指定了填充这个表的行的方法是FillRow 方法
14
//注意这个方法返回的一定是一个IEnumerable类型的,并且为公开,静态,这个方法的入参就是sql函数的入参
15
[SqlFunction(DataAccess = DataAccessKind.Read, TableDefinition = @"tid int ,unitname nvarchar(100),itemname nvarchar(100),
16
jan decimal(18,2),feb decimal(18,2), mar decimal(18,2), apr decimal(18,2),
17
may decimal(18,2),jun decimal(18,2), jul decimal(18,2), aug decimal(18,2),
18
sep decimal(18,2),oct decimal(18,2), nov decimal(18,2), dec decimal(18,2),
19
total decimal(18,2), flag int ", FillRowMethodName = "FillRow3")]
20
public static IEnumerable Fun_FactIncome_CLR(int iYear, string CompanyID, int type)
21
{
22
List<DataRow> rowList = new List<DataRow>();
23
DataTable dt = new DataTable();
24
#region 表结构
25
DataColumn col = new DataColumn("tid", typeof(int));
26
dt.Columns.Add(col);
27
dt.Columns.Add("unitname", typeof(string));
28
dt.Columns.Add("itemname", typeof(string));
29
30
AddColumns(ref dt, new string[] { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec", "total" });
31
32
col = new DataColumn("flag", typeof(int));
33
col.DefaultValue = 0;
34
dt.Columns.Add(col);
35
36
col = new DataColumn("myTemp", typeof(decimal));
37
col.Expression = "jan+feb+mar+apr+may+jun+jul+aug+sep+oct+nov+dec";
38
dt.Columns.Add(col);
39
#endregion
40
if (type == 0)//总部查询
41
{
42
string CompanyList = GetCompanyList(CompanyID);
43
string UnitName = "";
44
SearchCompany(iYear, CompanyID, rowList, ref dt, CompanyList, UnitName);
45
}
46
else if (type == 1)//区域查询
47
{
48
string CompanyList = GetCompanyList(CompanyID);
49
string UnitName = "";
50
SearchRegion(iYear, CompanyID, rowList, ref dt, CompanyList, UnitName);
51
}
52
else if (type == 2)//所有楼盘
53
{
54
string CompanyList = GetCompanyList(CompanyID);
55
string UnitName = "";
56
SearchBuilding(iYear, CompanyID, rowList, ref dt, CompanyList, UnitName);
57
}
58
else if (type == 3)//自定义楼盘查询
59
{
60
string CompanyList = GetUnitList(CompanyID);
61
string UnitName = "";
62
dt = SearchBuildingDef(iYear, rowList, dt, CompanyList);
63
}
64
else if (type == 4)//自定义区域查询
65
{
66
string CompanyList = GetUnitList(CompanyID);
67
string UnitName = "";
68
SearchRegionDef(iYear, CompanyID, rowList, ref dt, CompanyList, UnitName);
69
}
70
return rowList as IEnumerable;
71
//返回一个string 数组,这个数组符合IEnumerable接口,当然你也可以返回hashtable等类型。
72
73<#

要求用GridView显示,所以查询的结果要么是一个临时表或者是一个查询视图,想到sql2005中加入CLR的支持,有关CLR的操作请看体验:用C#写存储过程(VS.NET 2005) 或者在sqlserver2005中部署C#编写的自定义函数 ,实现的类如下
1
using System;2
using System.Collections.Generic;3
using System.Data;4
using System.Data.SqlClient;5
using System.Data.SqlTypes;6
using Microsoft.SqlServer.Server;7
using System.Collections;8

9
public partial class WYTableFunction10
{11
#region Fun_FactIncome_CLR12
//这个特性定义了一个sql表值函数,此函数返回的表的定义为:String nvarchar(200)13
//并且指定了填充这个表的行的方法是FillRow 方法14
//注意这个方法返回的一定是一个IEnumerable类型的,并且为公开,静态,这个方法的入参就是sql函数的入参15
[SqlFunction(DataAccess = DataAccessKind.Read, TableDefinition = @"tid int ,unitname nvarchar(100),itemname nvarchar(100),16
jan decimal(18,2),feb decimal(18,2), mar decimal(18,2), apr decimal(18,2),17
may decimal(18,2),jun decimal(18,2), jul decimal(18,2), aug decimal(18,2),18
sep decimal(18,2),oct decimal(18,2), nov decimal(18,2), dec decimal(18,2),19
total decimal(18,2), flag int ", FillRowMethodName = "FillRow3")]20
public static IEnumerable Fun_FactIncome_CLR(int iYear, string CompanyID, int type)21
{22
List<DataRow> rowList = new List<DataRow>();23
DataTable dt = new DataTable();24
#region 表结构25
DataColumn col = new DataColumn("tid", typeof(int));26
dt.Columns.Add(col);27
dt.Columns.Add("unitname", typeof(string));28
dt.Columns.Add("itemname", typeof(string));29

30
AddColumns(ref dt, new string[] { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec", "total" });31

32
col = new DataColumn("flag", typeof(int));33
col.DefaultValue = 0;34
dt.Columns.Add(col);35

36
col = new DataColumn("myTemp", typeof(decimal));37
col.Expression = "jan+feb+mar+apr+may+jun+jul+aug+sep+oct+nov+dec";38
dt.Columns.Add(col);39
#endregion40
if (type == 0)//总部查询41
{42
string CompanyList = GetCompanyList(CompanyID);43
string UnitName = "";44
SearchCompany(iYear, CompanyID, rowList, ref dt, CompanyList, UnitName);45
}46
else if (type == 1)//区域查询47
{48
string CompanyList = GetCompanyList(CompanyID);49
string UnitName = "";50
SearchRegion(iYear, CompanyID, rowList, ref dt, CompanyList, UnitName);51
}52
else if (type == 2)//所有楼盘53
{54
string CompanyList = GetCompanyList(CompanyID);55
string UnitName = "";56
SearchBuilding(iYear, CompanyID, rowList, ref dt, CompanyList, UnitName);57
}58
else if (type == 3)//自定义楼盘查询59
{60
string CompanyList = GetUnitList(CompanyID);61
string UnitName = "";62
dt = SearchBuildingDef(iYear, rowList, dt, CompanyList);63
}64
else if (type == 4)//自定义区域查询65
{66
string CompanyList = GetUnitList(CompanyID);67
string UnitName = "";68
SearchRegionDef(iYear, CompanyID, rowList, ref dt, CompanyList, UnitName);69
}70
return rowList as IEnumerable;71
//返回一个string 数组,这个数组符合IEnumerable接口,当然你也可以返回hashtable等类型。72

73<#



浙公网安备 33010602011771号