[数据库]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 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | |
|
| 公告 |
此Blog中的文章仅代表本人当时观点,不对其正确性做任何担保。转载原创,请知会作者。QQ MSN | |
| Blog信息 |
|
blog名称:倾程记 日志总数:27 评论数量:72 留言数量:-3 访问次数:234692 建立时间:2005年7月8日
| |

|