我有以下格式的CSV文件:
"FIELD1";"FIELD2";"FIELD3";"FIELD4";"FIELD5";"FIELD6";
"BABY";"333333";"0100-0100";"1";"3/2/2018";"88888";
"BABY";"333333";"0100-0200";"2";"3/3/2018";"88888";
"BABY";"444444";"0100-0300";"3";"3/4/2018";"88888";
我需要更新一个事务表和一个主表,其中事务表和主表有field2作为它们之间的公共,所以对于多行事务表,应该只有1行master,而事务表应该是首先更新,然后是主表。
下面的代码只读取2条记录并成功更新两个表,但是当读取第三行(最后一行)时,它只是跳过更新两个表。我需要帮助才能让代码正确更新文件中的所有记录。
Private Sub UpdateFromFile()
Dim strSql As New StringBuilder()
Dim strSql1 As New StringBuilder()
'Read the CSV file
Dim parser As New FileIO.TextFieldParser(path:="C:\IMPORTDATA\TEST.CSV") With {
.Delimiters = New String() {";"},
.HasFieldsEnclosedInQuotes = True,
.TrimWhiteSpace = True
}
Dim RowValues As String()
Dim PrevRowValues As String()
Dim PrevRefNumber As String = ""
'--First line is header, skip it
parser.ReadLine()
PrevRefNumber = ""
Dim IsFirstTime As Boolean = True
Do Until parser.EndOfData
Do While True
RowValues = parser.ReadFields()
If IsFirstTime Then
PrevRefNumber = RowValues(1)
IsFirstTime = False
End If
If PrevRefNumber <> RowValues(1) Then
Exit Do
End If
PrevRowValues = RowValues
LblStatus.Text = "Status: Writing Line " & i.ToString & "..."
strSql.AppendFormat("INSERT INTO ""Transaction"" (""Field1"", ""Field2"", ""Field3"", ""Field4"", ""Field5"") VALUES ('{0}','{1}','{2}',{3},'{4}')", RowValues(0), RowValues(1), RowValues(2), CDbl(RowValues(3)), RowValues(5))
MessageBox.Show(strSql.ToString)
Loop
LblStatus.Text = "Status: Writing Master File..."
strSql1.AppendFormat("INSERT INTO ""Master"" (""Field1"", ""Field2"", ""Field3"") VALUES ( '{0}', '{1}', '{2}')", PrevRowValues(0), PrevRowValues(1), PrevRowValues(5))
MessageBox.Show(strSql1.ToString)
PrevRefNumber = RowValues(1)
strSql1.Clear()
Loop
End Sub
End Class
答案 0 :(得分:0)
如果有人感兴趣,我终于让代码正常工作了......
'--TextField Parser is used to read the csv file
Dim parser As New FileIO.TextFieldParser(path:="C:\IMPORTDATA\TEST.CSV") With {
.Delimiters = New String() {";"}, ' fields are separated by semicolon
.HasFieldsEnclosedInQuotes = True, ' each of the values is enclosed with double quotes
.TrimWhiteSpace = True
}
Dim RowValues As String()
Dim PrevRowValues As String()
Dim PrevRefNumber As String = ""
Dim IsRefNumberSame As Boolean = False
Dim i As Integer = 1
'--First line is header, skip it
parser.ReadLine()
Dim IsFirstTime As Boolean = True
Dim IsTransWritten As Boolean = False
Do While Not parser.EndOfData
RowValues = parser.ReadFields
If IsFirstTime Then
PrevRefNumber = RowValues(1)
IsFirstTime = False
End If
If PrevRefNumber <> RowValues(1) Then
strSql1.AppendFormat("INSERT INTO ""master"" (""f1"", ""f2"", ""f3"") VALUES ( '{0}', '{1}', '{2}')", PrevRowValues(0), PrevRowValues(1), PrevRowValues(5))
MessageBox.Show(strSql1.ToString)
strSql1.Clear()
PrevRefNumber = RowValues(1)
End If
strSql.AppendFormat("INSERT INTO ""trans"" (""f1"", ""f2"", ""f3"", ""f4"", ""f5"", ""f6"") VALUES ('{0}','{1}','{2}',{3},'{4}',{5} )", RowValues(0), RowValues(1), RowValues(2), CDbl(RowValues(3)), RowValues(5), 1)
MessageBox.Show(strSql.ToString)
strSql.Clear()
PrevRowValues = RowValues
Loop
strSql1.AppendFormat("INSERT INTO ""master"" (""f1"", ""f2"", ""f3"") VALUES ( '{0}', '{1}', '{2}')", RowValues(0), RowValues(1), RowValues(5))
MessageBox.Show(strSql1.ToString)