如果我在 SQL Server Management Studio 中执行 MDX 查询,我会在 2 秒内得到该查询的结果。它返回大约 400 行 6 列。
当我通过 ADOMD 执行相同的查询并在 cellset 上循环时,大约需要 5 分钟。
使用 ADOMD 检索数据的最快方法是什么以及为什么这种方法需要这么长时间?
我正在使用以下代码。
namespace Delete
{
public class TreeNode
{
public string MemberName { get; set; }
public string ID { get; set; }
public string ParentKey { get; set; }
public int Level { get; set; }
public string HierarchyLevel { get; set; }
public bool root { get; set; }
public bool leaf { get; set; }
public bool expanded
{
get { return true; }
}
public bool @checked
{
get { return false; }
}
public List<TreeNode> children { get; set; }
}
class Program
{
static void Main(string[] args)
{
var v = ExecuteQueryForHierarchy("", "");
}
private static List<TreeNode> ExecuteQueryForHierarchy(string connString, string query)
{
Stopwatch sw = new Stopwatch();
sw.Start();
CellSet cellset;
connString = "";
query = @"";
List<TreeNode> treeNodeList = new List<TreeNode>();
var connection = new AdomdConnection(connString);
var command = new AdomdCommand(query, connection);
try
{
connection.Open();
cellset = command.ExecuteCellSet();
TreeNode node = null;
var positionCollection = cellset.Axes[1].Positions;
foreach (var item in positionCollection)
{
node = new TreeNode();
node.MemberName = item.Members[0].Caption;
node.Level = item.Members[0].LevelDepth;
node.HierarchyLevel = item.Members[0].LevelName;
node.ParentKey = item.Members[0].Parent != null ? item.Members[0].Parent.UniqueName : null;
node.root = item.Members[0].Parent == null ? true : false;
node.leaf = item.Members[0].ChildCount <= 0;
node.ID = item.Members[0].UniqueName;
treeNodeList.Add(node);
Console.WriteLine(treeNodeList.Count);
}
}
finally
{
connection.Close();
connection.Dispose();
}
sw.Stop();
TimeSpan elapsedTime = sw.Elapsed;
Console.WriteLine(sw.Elapsed.ToString());
Console.ReadKey();
return treeNodeList;
}
private List<TreeNode> BuildTree(IEnumerable<TreeNode> items)
{
List<TreeNode> itemL = items.ToList();
itemL.ForEach(i => i.children = items.Where(ch => ch.ParentKey == i.ID).ToList());
return itemL.Where(i => i.ParentKey == null).ToList();
}
}
}