CellSet转换成DataTable

在ADOMD.NET下返回的CellSet结果通常来说是无法直接用到容器控件当中的,因为其并没有实现相应的接口,所以通常需要转换成DataTable然后再做处理。

以下代码收集自网络

 1None.gif    public DataTable ToDataTable(CellSet cs)
 2ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
 3InBlock.gif        DataTable dt = new DataTable();
 4InBlock.gif        dt.TableName = "resulttable";
 5InBlock.gif        DataColumn dc = new DataColumn();
 6InBlock.gif        DataRow dr = null;
 7InBlock.gif
 8InBlock.gif        //第一列:必有为维度描述(行头)
 9InBlock.gif        dt.Columns.Add(new DataColumn("Description"));
10InBlock.gif
11InBlock.gif        //生成数据列对象
12InBlock.gif        string name;
13InBlock.gif
14InBlock.gif        foreach (Position p in cs.Axes[0].Positions)
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            dc = new DataColumn();
17InBlock.gif            name = "";
18InBlock.gif            foreach (Member m in p.Members)
19ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
20InBlock.gif                name = name + m.Caption + " ";
21ExpandedSubBlockEnd.gif            }

22InBlock.gif
23InBlock.gif            dc.ColumnName = name;
24InBlock.gif            dt.Columns.Add(dc);
25ExpandedSubBlockEnd.gif        }

26InBlock.gif
27InBlock.gif        //添加行数据
28InBlock.gif        int pos = 0;
29InBlock.gif
30InBlock.gif        foreach (Position py in cs.Axes[1].Positions)
31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
32InBlock.gif            dr = dt.NewRow();
33InBlock.gif
34InBlock.gif            //维度描述列数据(行头)
35InBlock.gif            name = "";
36InBlock.gif
37InBlock.gif            foreach (Member m in py.Members)
38ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
39InBlock.gif                name = name + m.Caption + "\r\n";
40ExpandedSubBlockEnd.gif            }

41InBlock.gif            dr[0= name;
42InBlock.gif
43InBlock.gif            //数据列
44InBlock.gif            for (int x = 1; x <= cs.Axes[0].Positions.Count; x++)
45ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
46InBlock.gif                dr[x] = cs[pos++].FormattedValue;
47ExpandedSubBlockEnd.gif            }

48InBlock.gif            dt.Rows.Add(dr);
49ExpandedSubBlockEnd.gif        }

50InBlock.gif        return dt;
51ExpandedBlockEnd.gif    }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值