【发布时间】:2012-09-18 15:38:06
【问题描述】:
我在将 CSV 文件上传到我的 SQL 数据库时遇到问题。我正在尝试通过 Intranet 站点上的文件上传技术来实现这一点。 Intranet 站点是为我和另一个用户提供文件上传这些 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 Profiler 来查看实际访问数据库的 SQL。此外,您的 CSV 导入可能存在问题。我最近使用了这个 CSV 库并喜欢它:github.com/JoshClose/CsvHelper。它也可以在 NuGet 上使用。
-
另外,显示更多代码,并确保格式化。
-
如果您使用相同的连接发出选择语句会发生什么:SQLCmd.CommandText = "SELECT * [Base].[PartsImport]";
-
我执行了 SELECT 语句,但仍然对 SQL 数据库没有任何区别或添加。我添加了更多我的代码。希望这能让您更清楚地了解问题所在。
标签: asp.net sql-server vb.net csv intranet