今天在调试程序的时候,发现将DataTable加到DataSet引起的错误,可能有的人会说这不是很简单吗!做VB.net开发的基本上都会用到,而且也没出什么异常呀!不过就是将一个DataSet中的DataTable直接追加到另一个DataSet中出现Bug了,
请看下面代码:
Business层代码
Public Shared Function GetCSVInfo(ByVal p_strOrderRequestNo As String) As DataSet
Dim l_dstOrderCSV As New DataSet
Dim l_objHeaderDataAccess As New VendorOrderRequest_Access
Dim l_objDetailDataAccess As New VendorOrderRequestDetail_Access
![](/Images/OutliningIndicators/InBlock.gif)
l_dstOrderCSV.Tables.Add(l_objHeaderDataAccess.GetOrderCSVHeaderInfo(p_strOrderRequestNo))
l_dstOrderCSV.Tables.Add(l_objDetailDataAccess.GetOrderCSVDetailInfo(p_strOrderRequestNo))
![](/Images/OutliningIndicators/InBlock.gif)
Return l_dstOrderCSV
End Function
Access层代码:
Public Function GetOrderCSVHeaderInfo(ByVal p_strOrdertRequestNo As String) As DataTable
Dim l_objDataSet As New DataSet
'执行存储过程代码
l_objDataAdapter.Fill(l_objDataSet, "CSVHeaderInfo")
![](/Images/OutliningIndicators/InBlock.gif)
Return l_objDataSet.Tables(0)
End Function
其实上面的代码是有问题的:将一个DataSet中的DataTable直接追加到另一个DataSet中是不允许的,因为追加时是Table的地址传递,那么两个DataSet同用一个DataTable,DataSet中是不允许这样的操作。
解决方法:
1、DataSet中的Table Copy一份追加到另一个DataSet中
DataSet1.Tables.add(DataSet2.Tables(0).Copy)
2、Table单独存在,不要放到DataSet中
DataSet1.Tables.add(Table1)
Keyword:datatable,dataset,dataset.tables,dataset.add,datatable.copy,
请看下面代码:
Business层代码
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
Access层代码:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
解决方法:
1、DataSet中的Table Copy一份追加到另一个DataSet中
DataSet1.Tables.add(DataSet2.Tables(0).Copy)
2、Table单独存在,不要放到DataSet中
DataSet1.Tables.add(Table1)
Keyword:datatable,dataset,dataset.tables,dataset.add,datatable.copy,