我正在解析固定長度文本文件的每一行。我從這個文本文件中檢索13個值,並將它們插入到多個數據類型的變量中。最終我需要將這些值添加到數據表中的某一行,但問題是我不知道如何執行此操作。我嘗試循環遍歷列,並嘗試在右列插入正確的變量。 How do I add each of the variables below to the corresponding column in a datatable all in the same row?
將變量插入到數據表的相應列中vb.net
這裏是我的變量:
Dim vendorName As String 'a
Dim vendorNum As Integer 'b
Dim CheckNum As String 'c - with checkNum and batch no combined
Dim depositDate As String 'd
Const crAccount As Integer = 102006 'e
Dim totCount As Integer ' f
Const blank As String = "" 'g
Const num As Integer = 1 'h
Dim qtr As Integer 'i
Dim custName As String 'j
Dim acctNum As Integer 'k
Dim headerAmount(headerCounter) As Double ' '// Not in the list
Dim detailAmount As Double 'l
Dim state As String 'm
Dim batchNo As String 'n -set this to string to retrieve the 00 or 01 or however many batches there is.
Dim checkNo As String 'o
這裏是我的專欄:
dt.Columns.Add("Vendor #")
dt.Columns.Add("Vendor Name")
dt.Columns.Add("Check #")
dt.Columns.Add("Date")
dt.Columns.Add("102006")
dt.Columns.Add("#")
dt.Columns.Add("Blank")
dt.Columns.Add("1")
dt.Columns.Add("12QTR")
dt.Columns.Add("Client Name")
dt.Columns.Add("Acct No")
dt.Columns.Add("Amt")
dt.Columns.Add("State")
dt.Columns.Add("Batch No")
dt.Columns.Add("Check Number")
這裏是我試過的循環,但將每個變量它自己的行:
For Each col As DataColumn In dt.Columns
If col.ColumnName = "Vendor #" Then
dt.Rows.Add(vendorNum)
ElseIf col.ColumnName = "Vendor Name" Then
dt.Rows.Add(vendorName)
ElseIf col.ColumnName = "Check #" Then
dt.Rows.Add(checkNo)
ElseIf col.ColumnName = "Date" Then
dt.Rows.Add(depositDate)
ElseIf col.ColumnName = "102006" Then
dt.Rows.Add(crAccount)
ElseIf col.ColumnName = "#" Then
dt.Rows.Add(totCount)
ElseIf col.ColumnName = "Blank" Then
dt.Rows.Add(blank)
ElseIf col.ColumnName = "1" Then
dt.Rows.Add(num)
ElseIf col.ColumnName = "12QTR" Then
dt.Rows.Add(qtr)
ElseIf col.ColumnName = "Client Name" Then
dt.Rows.Add(custName)
ElseIf col.ColumnName = "Acct No" Then
dt.Rows.Add(acctNum)
ElseIf col.ColumnName = "Amt" Then
dt.Rows.Add(detailAmount)
ElseIf col.ColumnName = "State" Then
dt.Rows.Add(state)
ElseIf col.ColumnName = "Batch No" Then
dt.Rows.Add(batchNo)
ElseIf col.ColumnName = "Check Number" Then
dt.Rows.Add(CheckNum)
End If
'dt.Rows.Add(vendorNum & vendorName & checkNo & depositDate &
'crAccount & totCount & blank & num & qtr & custName & acctNum
'& detailAmount & state & batchNo & CheckNum)
'// I also tried concatenating each of the parameters together
' and when I do this it is only trying to insert in the Vendor # column.
Next
這似乎並不像它窩這太難了,但我找不到任何這樣的例子。任何幫助,將不勝感激。謝謝
因此,這將是類似於我在我的代碼,'dt.Rows.Add底部註釋掉線(vendorNum& vendorname&checkNo&depositDate&crAccount&totCount&blank&num&qtr&custName&acctNum&detailAmount&state&batchNo&CheckNum)''但是我只需要首先將這些元素添加到數組中? –
是的。但是你不得不使用一個Object數組,因爲你有不同類型的Variables。我建議使用Typed DataSets並將其綁定到DataGridView(如果GUI需要) – Markus
如果我創建了一個類型化數據集,我將如何將15個變量插入到它中?我使用數據集的唯一方法是創建一個xsd,並將數據表添加到數據集。 –