倾程记 flyby      本站首页     管理页面    写新日志    退出   


[数据库]ADOMD使用的例子
flyby 发表于 2007/1/7 0:10:46

我今天找到了在C#中连接ADO(MD)的例子。很简单好用!1 创建一个web项目;2 添加Com引用Microsoft ADO(MD)2.8;3 在web页面上添加一个按钮和一个文本框(多行);4 点击“按钮”添加事件代码如下: string query;// Assign the MDX query to a string.query = TextBox1.Text.ToString();// Create an ADO MD catalog and an ADO MD Cellset.ADOMD.Catalog oAdoMDCat = new ADOMD.CatalogClass();ADOMD.Cellset oAdoMDCellSet = new ADOMD.Cellset();try{    // Open a connection to the Analysis Server,     // and then run the MDX query.    oAdoMDCat.let_ActiveConnection("Provider=MSOLAP.1;" +         "Data Source=localhost;" +         "Initial Catalog=Foodmart 2000");    oAdoMDCellSet.Open(query, oAdoMDCat.ActiveConnection);     // Create the pos object to contain two ordinals     // for the coordinate.    int numaxes = oAdoMDCellSet.Axes.Count;    object[] pos = new object[numaxes];    int posval = 140;     // Traverse through the pages.    for (int pages = 0; pages <= oAdoMDCellSet.Axes.Count ; pages++)    {         // Traverse through the columns to write a table header.        Response.Write("<Table style =" + '"' + "Z-ORDER = 103; LEFT: 20px;" + " WIDTH:" +    "800px; POSITION: absolute; TOP: " + posval.ToString()+"px" + '"' + "HEIGHT: 110px" + "cellSpacing=1 cellPadding=1 width=600 border=1> <TR>");        for (int ia = 0; ia <= oAdoMDCellSet.Axes[0].Positions.Count - 1; ia++)        {// If a page exists, write the name of the page.if (numaxes>2)    Response.Write("<TD>" +         oAdoMDCellSet.Axes[2].Positions[pages].Members[0].        Caption.ToString()         + "</TD>");// Write the name of the columns.Response.Write("<TD>" +     oAdoMDCellSet.Axes[0].Positions[ia].Members[0].    Caption.ToString()+    "</TD>");        }        Response.Write("</TR>");         // Traverse through the rows.        for (int j = 0; j <=oAdoMDCellSet.Axes[1].Positions.Count - 1; j ++)        {// Write the dimension member name.Response.Write("<TR><TD>" +     oAdoMDCellSet.Axes[1].Positions[j].Members[0].    Caption.ToString() +     "</TD>"); // Traverse through the columns.for (int i = 0; i <= oAdoMDCellSet.Axes[0].Positions.Count - 1;     i++){    // Load the cell coordinates to the pos object.    pos[0] = System.Convert.ToInt16(oAdoMDCellSet.Axes[0].        Positions[i].Ordinal.ToString());    pos[1] = System.Convert.ToInt16(oAdoMDCellSet.Axes[1].        Positions[j].Ordinal.ToString());    if (numaxes>2)        pos[2] = System.Convert.ToInt16(oAdoMDCellSet.Axes[2].Positions[pages].Ordinal.ToString());    // Retrieve the value of a cell.    ADOMD.Cell cell = oAdoMDCellSet.get_Item(ref pos);     // Write the FormattedValue property.    Response.Write("<TD>" + cell.FormattedValue + "</TD>");    posval=posval+25;}Response.Write("</TR>");        }        Response.Write("</Table>");    }    // Close the Cellset.    oAdoMDCellSet.Close();}catch (Exception goof){    string err_message;    err_message=goof.Message.ToString();    Response.Write(err_message.ToString());} // Release the Cellset object and the Catalog object from the memory.System.Runtime.InteropServices.Marshal.    ReleaseComObject(oAdoMDCellSet);System.Runtime.InteropServices.Marshal.    ReleaseComObject(oAdoMDCat);GC.Collect();GC.GetTotalMemory(true);GC.WaitForPendingFinalizers(); 5 编译,然后在文本框中输入: select 500)this.width=500'>{[Measures].Members} on columns, 500)this.width=500'>{[Promotion Media].Members} on rows 500)this.width=500'>from Sales

阅读全文(2068) | 回复(0) | 编辑 | 精华

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)

 
«December 2025»
123456
78910111213
14151617181920
21222324252627
28293031

  公告

此Blog中的文章仅代表本人当时观点,不对其正确性做任何担保。转载原创,请知会作者。QQ  MSN


  我的分类(专题)
  最近日志

  最新评论

  留言板

  链接

  Blog信息

blog名称:倾程记
日志总数:27
评论数量:72
留言数量:-3
访问次数:234692
建立时间:2005年7月8日



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.799 second(s), page refreshed 144796708 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号