2011-07-13 40 views
0

我有一個報告,我需要相同的數據,但爲四個單獨的日期範圍。目前這個數據基於一個月,所以它只是一個數據集。但是現在我試圖用四個獨立的數據集來完成這個工作,每個數據集都在四個日期範圍中的每一個上進行過濾。我設置我的WHILE循環來循環所有四個日期。但是我現在遇到了一些麻煩,因爲它們命名了這些數據集。這是做這件事的最好方法嗎?看起來如果我爲一個數據集使用多個數據表,我必須定義每個數據表列。這是我目前的數據集的CommandString:如何在VB.NET中編寫多個數據集?

commandstring = "SELECT Batch_Records.Part_Number, Batch_Records.Lot_Number, Batch_Records.Date_Received, " & _ 
              "IsNull([Date_Completed], [Review_Date]) AS CompleteDate, Batch_Records.Error, " & _ 
              "Batch_Records.[Group], Batch_Records.MFG, Batch_Records.MFG2, Batch_Records.QC, Batch_Records.QC2, " & _ 
              "QC_CODES.CODE_DESC " & _ 
              "FROM EXCEL.Batch_Records LEFT JOIN EXCEL.QC_CODES ON Batch_Records.Part_Number = QC_CODES.CODE_ID " & _ 
              "WHERE (Batch_Records.[Group]" & TheGroup & " AND Batch_Records.Date_Received > '" & arrWeekYear(i, j).ToString("d") & "' AND Batch_Records.Date_Received < '" & arrWeekYear(i + 7, j).ToString("d") & "')" 
+1

爲什麼不能針對您有興趣根據需要對參數化開始和結束日期的四個日期範圍進行四種不同的查詢? –

+0

同意馬特。參數化開始和結束日期並創建一個存儲過程。並將信息保存在一個DataSet中的4個DataTable中。 –

回答

0

如果我理解正確的話,你要4個範圍在一個查詢:

延長WHERE(並觀看了括號...)

"WHERE (Batch_Records.[Group]" & TheGroup & 
" AND (Batch_Records.Date_Received > '" & arrWeekYear(i1, j).ToString("d") & "' AND Batch_Records.Date_Received < '" & arrWeekYear(i1 + 7, j).ToString("d") & 
") or (Batch_Records.Date_Received > '" & arrWeekYear(i2, j).ToString("d") & "' AND Batch_Records.Date_Received < '" & arrWeekYear(i2 + 7, j).ToString("d") & 
") or (Batch_Records.Date_Received > '" & arrWeekYear(i3, j).ToString("d") & "' AND Batch_Records.Date_Received < '" & arrWeekYear(i3 + 7, j).ToString("d") & 
") or (Batch_Records.Date_Received > '" & arrWeekYear(i4, j).ToString("d") & "' AND Batch_Records.Date_Received < '" & arrWeekYear(i4 + 7, j).ToString("d") & 
"'))" 

或者如果您想要有4組輸出,請創建一個參數化查詢。只需更新參數值即可運行4次。因此,您只有1個查詢就沒有命名問題。

相關問題