2016-11-22 105 views
0

我正在處理一些針對sql server分析服務執行MDX查詢的代碼。 MDX查詢在同一個線程上執行兩次,我不知道爲什麼。查詢應該只執行一次c#線程/查詢執行兩次

下面是一些代碼。任何人都可以幫助請。

 private void Start_QueryWorkers() 
    {    
      foreach (QueryThread th in _QueryWorkers) 
      { 
      SSASQuery q = new SSASQuery();      
      q.QueryText = "SELECT NON EMPTY { [Measures].[count] } ON COLUMNS FROM [cube]";         
      q.queryThread = th;    
      th.WorkerThread.RunWorkerAsync(q); 
     }   
    } 

    private void QueryWorkerThread_DoWork(object sender, DoWorkEventArgs e) 
    { 
     SSASQuery q = e.Argument as SSASQuery;  
     OleDbCommand cmd = new OleDbCommand(q.QueryText, q.queryThread.conn); 
     OleDbDataReader rdr = cmd.ExecuteReader();    
      rdr.Close();   
    } 

     private void btnStartTest_Click(object sender, EventArgs e) 
    {  
      string strConnString = "Provider=MSOLAP;Data Source=SRV001" + ";Initial Catalog=Cube_2015" + ";"; 
     _QueryWorkers.Clear(); 
     {     
      QueryThread thread = new QueryThread(strConnString);   
      thread.WorkerThread = new BackgroundWorker(); 
       thread.WorkerThread.DoWork += new DoWorkEventHandler(this.QueryWorkerThread_DoWork);         
       _QueryWorkers.Add(thread);  
     }        
      Start_QueryWorkers();    
    }    
} 

class SSASQuery 
{ 
    public string QueryText { get; set; }  
    public QueryThread queryThread { get; set; }   
} 

class QueryThread 
{ 
    public QueryThread(string connString) 
    { 
     this.connString = connString; 
     conn = new OleDbConnection(connString); 
     conn.Open();   
     queryList = new SortedList(); 
    } 
    public SortedList queryList { get; set; } 
    public string threadName { get; set; } 
    public string connString { get; set; } 
    public OleDbConnection conn;  
    public BackgroundWorker WorkerThread { get; set; }  
    } 

}

+0

BackgroundWorker不是線程,它是對線程的過時抽象。通過嘗試使用它,就好像它是一個線程一樣,你什麼也得不到。使用PLINQ,Parallel.ForEach或ActionBlock 來並行執行每個查詢並將結果存儲到任何要存儲它們的地方會更容易 –

+0

嗨,好的謝謝您的建議。無論如何,你可以幫助爲什麼DoWork/BackgroundWorker被執行兩次在上面的代碼 – DC07

+0

爲什麼你使用'for(int i = 0;我 McNets

回答

0

解決。代碼是好的,這是連接字符串執行查詢兩次的問題