VB.NET Txtfile -> SQL 服务器循环问题



因此,我尝试使用 VB.NET 读取文本文件,并将每行的值添加到SQL Server数据库表中。到目前为止,我拥有的代码在第一行中读取,但一直尝试在同一行中读取并失败。它添加了第一个,但就像我所说的那样,它在第一个之后失败。我很确定问题出在我的循环和/或 mysplit() 数组值中。任何正确方向的帮助将不胜感激。谢谢

Public Sub addTxtMember()
    'Dim fileName As String
    ' fileName = Application.StartupPath + "c:usersjames needhamdocumentsvisual studio 2013projectstextfiletutorial1textfiletutorial1textfile1.txt"
    Dim fileName As Integer = FreeFile()
    'If File.Exists(fileName) Then
    'Dim iofile As New StreamReader(fileName)
    FileOpen(fileName, "TextFile1.txt", OpenMode.Input)
    Dim ioline As String
    'Dim iolines() As String
    Dim eID, fName, lName, email, phone
    Dim i As Integer = 0
    ioline = LineInput(fileName)
    'Do Until EOF(fileName)
    While Not ioline = ""
        ' Dim endsplit = Split(ioline, "")
        Dim mysplit = Split(ioline, ",")
        eID = mysplit(0)
        fName = mysplit(1)
        lName = mysplit(2)
        phone = mysplit(3)
        email = mysplit(4)
        ' try
        Dim strInsert As String = "INSERT INTO Employees1 (eID, fName, lName, phone, email) " & _
                                 "VALUES (" & _
                                 "'" & eID & "'," & _
                                 "'" & fName & "', " & _
                                 "'" & lName & "', " & _
                                 "'" & phone & "', " & _
                                 "'" & email & "')"
        'MsgBox(strInsert)
        SQLCon.Open()
        SQLCmd = New SqlCommand(strInsert, SQLCon)
        'has to be non when inserting, updating, or deleting
        SQLCmd.ExecuteNonQuery()
        SQLCon.Close()
        ' Catch ex As Exception
        'MsgBox(ex.Message)
        'End Try
    End While
    FileClose(fileName)
    'Else
    'MsgBox("fILE NOT FOUND")
    ' FileClose(fileName)
    'End If
End Sub

以下评论绝不意味着我相信这个程序会工作——它有很多错误,但要回答你关于只看到一行出现的问题:

添加行

ioline = LineInput(fileName)

END WHILE

你要求轻推你的循环......

    For Each line As String In (New StreamReader("TextFile1.txt")).ReadToEnd.Split(ControlChars.CrLf)
        'examine the value of line and parse it into the DB here
    Next

最新更新