如何在VB.NET中编写多个数据集?

时间:2011-07-13 13:32:37

标签: vb.net dataset

我有一份报告,我需要相同的数据,但有四个不同的日期范围。目前这个数据基于一个月,所以它只是一个数据集。但现在我尝试使用四个单独的数据集来执行此操作,其中每个数据集都在四个日期范围中的每一个上进行过滤。我设置我的WHILE循环以循环到所有四个日期。但是现在我遇到了这些数据集的不同命名问题。这是最好的方法吗?看起来如果我为一个数据集使用多个数据表,我必须定义每个数据表列。这是我当前的数据集命令字符串:

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 :(得分: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个查询,因此没有命名问题。