将行添加到DataTable而不会丢失先前的行

时间:2012-04-14 09:12:57

标签: asp.net vb.net

Dim  _tableBackLogs As System.Data.DataTable

Do While i - 2 > 0
_tableBackLogs = Global.DataAccess.GetDataTable("SELECT SubjectID,SubjectName,Grade FROM SubjectPI WHERE RegNo='" & CInt(HttpContext.Current.Session("userName")) & "' AND Status='Fail' AND Semester='" & i - 2 & "'")
i = i - 2
Loop

这样做会取代DataTable中的先前数据。我想保留以前的数据,即我希望将新行添加到DataTable中,而不是替换之前的行。

2 个答案:

答案 0 :(得分:1)

用以下内容替换您的代码。

Dim _tableBackLogs As New System.Data.DataTable

Do While i - 2 > 0
    _tableBackLogs.Merge(Global.DataAccess.GetDataTable("SELECT SubjectID,SubjectName,Grade FROM SubjectPI WHERE RegNo='" & CInt(HttpContext.Current.Session("userName")) & "' AND Status='Fail' AND Semester='" & i - 2 & "'"))
    i = i - 2
Loop

答案 1 :(得分:0)

  

使用合并方法

Dim  _tableBackLogs As System.Data.DataTable

Do While i - 2 > 0
_tableBackLogs.Merge(Global.DataAccess.GetDataTable("SELECT SubjectID,SubjectName,Grade FROM SubjectPI WHERE RegNo='" & CInt(HttpContext.Current.Session("userName")) & "' AND Status='Fail' AND Semester='" & i - 2 & "'"))
i = i - 2
Loop