SQL 데이터베이스에 CSV 파일을 업로드하는 데 문제가 있습니다. 필자는 인트라넷 사이트에서 파일 업로드 기술을 통해이 작업을 수행하려고합니다. 인트라넷 사이트는 나와 다른 사용자가이 CSV 파일을 업로드 할 수있는 기능을 제공합니다.파일 작성 방법 인트라넷 VB.NET 사이트의 SQL 데이터베이스에 csv 업로드
다음을 사용했습니다.
Dim errorList As String = String.Empty
Dim returnValue As Integer = 0
Dim SQLCon As New SqlClient.SqlConnection
Dim SQLCmd As New SqlClient.SqlCommand
Dim ErrString As String
Dim countRecs As Integer = 0
Dim batchid As Integer = GetNextBatchNumber("PartsImport")
Using tf As New TextFieldParser(fileandpath)
tf.TextFieldType = FileIO.FieldType.Delimited
tf.SetDelimiters(",")
SQLCon.ConnectionString = ConfigurationManager.ConnectionStrings("DB00ConnectionString").ConnectionString
SQLCon.Open()
SQLCmd.CommandType = CommandType.Text
SQLCmd.Connection = SQLCon
Dim recAdded As String = Now.ToString
Dim row As String()
While Not tf.EndOfData
Try
row = tf.ReadFields()
Dim x As Integer = 0
If countRecs <> 0 Then
Try
SQLCmd.CommandText = "insert into [Base].[PartsImport] " _
+ " (ID,PartName,PartID,Price,ShipAddress) " _
+ " values ('" + row(0) + "','" + row(1) + "','" _
+ row(2) + "','" + row(3) + "','" + row(4) + "')"
SQLCmd.ExecuteNonQuery()
Catch ex As Exception
ErrString = "Error while Creating Batch Record..." & ex.Message
End Try
End If
Catch ex As MalformedLineException
errorList = errorList + "Line " + countRecs + ex.Message & "is not valid and has been skipped." + vbCrLf
End Try
countRecs = countRecs + 1
End While
SQLCon.Close()
SQLCon.Dispose()
SQLCmd.Dispose()
업로드 할 때 양식 버튼을 클릭하면 성공 메시지가 표시되지만 실제 표를 보면 여전히 비어 있습니다.
아이디어가 있으십니까? 그것을 감사
감사 데이브
표시되지 않는 오류가있을 수 있습니다. SQL 프로필러를 실행하여 실제로 어떤 SQL이 데이터베이스에 도달했는지 확인할 수 있습니다. 또한 CSV 가져 오기에 문제가있을 수 있습니다. 나는 최근에이 CSV 라이브러리를 사용했고 그것을 사랑했습니다 : https://github.com/JoshClose/CsvHelper. NuGet에서도 사용할 수 있습니다. – Gromer
또한 더 많은 코드를 표시하고 서식을 지정해야합니다. – Gromer
동일한 연결을 사용하여 select 문을 실행하면 어떻게됩니까? SQLCmd.CommandText = "SELECT * [Base]. [PartsImport]"; –