Sql server VB.NET Txtfile->SQL server循环问题
因此,我尝试使用VB.NET读取文本文件,并将每行的值添加到SQL Server数据库表中。到目前为止,我的代码读取第一行,但一直尝试读取同一行,结果失败。它添加了第一个,但正如我所说的,它在第一个之后失败。我很确定问题出在我的循环和/或mysplit数组值中。如能在正确的方向上给予帮助,我们将不胜感激。多谢各位Sql server VB.NET Txtfile->SQL server循环问题,sql-server,vb.net,Sql Server,Vb.net,因此,我尝试使用VB.NET读取文本文件,并将每行的值添加到SQL Server数据库表中。到目前为止,我的代码读取第一行,但一直尝试读取同一行,结果失败。它添加了第一个,但正如我所说的,它在第一个之后失败。我很确定问题出在我的循环和/或mysplit数组值中。如能在正确的方向上给予帮助,我们将不胜感激。多谢各位 Public Sub addTxtMember() 'Dim fileName As String ' fileName = Application.StartupPa
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
下面的评论绝不意味着我相信这个程序会起作用——它有很多错误,但要回答您关于只看到一行显示的问题: 添加行
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
您从未重新读取循环中的行。您没有将文件中的任何内容读入ioline。您还需要了解SQL参数。如果您刚刚开始,请不要使用i作为变量名!投票以键入答案的方式结束不太可能对未来的读者有所帮助。您是否曾尝试在调试器中运行此操作?