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

时间:2015-04-22 20:26:53

标签: sql-server vb.net

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

Public Sub addTxtMember()
    'Dim fileName As String
    ' fileName = Application.StartupPath + "c:\users\james needham\documents\visual studio 2013\projects\textfiletutorial1\textfiletutorial1\textfile1.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

2 个答案:

答案 0 :(得分:0)

以下评论绝不意味着我相信这个程序会起作用 - 它有很多错误,但回答你的问题,只看到一行显示:

添加行

ioline = LineInput(fileName)

之前的

END WHILE

答案 1 :(得分:0)

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

    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