我有一个从.csv填充的数据网格,.csv是逗号分隔的。 csv在字符串字段中还包含逗号,以至于将其加载到datagrid中时变得混乱,而csv文件中还包含字符',该字符与稍后提交给数据库时发生混乱。
不是在加载之前“清理”文件,而是有一种简单的方法来扫描加载的网格并删除这些字符吗?还是先清理文件是更好的方法?
预先感谢
在加载之前清理文件似乎可以正常工作。
加载代码:
Dim TextLine As String = ""
Dim SplitLine() As String
If System.IO.File.Exists(fname) = True Then
Dim objReader As New System.IO.StreamReader(fname)
Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
SplitLine = Split(TextLine.Replace("""", ""), ",")
'SplitLine = Split(TextLine, ",")
grdDisplay.ColumnCount = SplitLine.Count
grdDisplay.Rows.Add(SplitLine)
Loop
objReader.Close()
objReader.Dispose()
grdDisplay.AutoResizeColumns()
Else
MessageBox.Show("File Does Not Exist")
End If
提交数据库代码:
Dim cm作为新的SqlCommand 与厘米 .Connection = cn
For i As Integer = 1 To grdDisplay.RowCount - 1
.CommandText = "insert into DefectRequestEntries values
('" & grdDisplay.Rows(i).Cells(0).Value & "',' " & grdDisplay.Rows(i).Cells(1).Value & "','
" & grdDisplay.Rows(i).Cells(2).Value & " ',' " & grdDisplay.Rows(i).Cells(3).Value & "','
" & grdDisplay.Rows(i).Cells(4).Value & " ',' " & grdDisplay.Rows(i).Cells(5).Value & "','
" & grdDisplay.Rows(i).Cells(6).Value & " ',' " & grdDisplay.Rows(i).Cells(7).Value & "','
" & grdDisplay.Rows(i).Cells(8).Value & " ',' " & grdDisplay.Rows(i).Cells(9).Value & "','
" & grdDisplay.Rows(i).Cells(10).Value & " ',' " & grdDisplay.Rows(i).Cells(11).Value & "','
" & (grdDisplay.Rows(i).Cells(12).Value & "')")
.ExecuteNonQuery()
Next
End With