OleDbDataAdapter - 读取制表符分隔文件
OleDbDataAdapter - read tab delimited file
(我不需要 OleDbDataAdapter 的替代品。)
下面的代码可以找到并读取文件,但 DGV 有四列(如预期的那样),但所有数据行的第一列只有文本。
Dim sDir As String = "c:\temp\"
Dim sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sDir & ";Extended Properties='text;HDR=Yes;FMT=TabDelimited';"
Dim dt As New DataTable()
Using adapt As New OleDbDataAdapter(String.Format("SELECT TOP 100 * FROM robo.txt"), sConn)
adapt.Fill(dt)
End Using
DataGridView1.DataSource = dt
我认为扩展属性是唯一的要求。我尝试添加 Schema.ini 无济于事 - 我认为它甚至没有被读取,因为列 header 与模式不匹配。
最成功的传递中的 header 行使用逗号作为分隔符 - 这导致四列具有正确的名称,但制表符将所有数据分隔在 Col1 中。如果我在 header 行中使用制表符,我会得到一些系统分配列 (3),它对应于带有两个逗号的数据行。
我做错了什么?
这是制表符被替换为 <tab>
的前几行。从那以后,我注意到我在数据中多了一列。对下方 header 行的修复并未解决问题 - 所有数据都转储到第一个字段中。
在 header 中使用制表符分隔符而不是逗号,结果所有 header 文本和数据都被转储到第一个字段。
col1,state,col3,size,path
<tab> same<tab><tab> 102912<tab>\APCD04T\Data\Thumbs.db
<tab> same<tab><tab> 22016<tab>\APCD04T\Data\APCD Topical Info\APCD_Boards&Committees_List.doc
<tab> same<tab><tab> 4.3 m<tab>\APCD04T\Data\APCD Topical Info\LOSSAN-LAtoSLORailCorridorStrategicPlan.pdf
在尝试使用 OLEDB 将 RoboCopy
日志加载到 DataTable
时学到了一些东西。
- 日志文件需要有 .txt 或 .csv(或?)扩展名,.log 失败。
- Schema.ini 似乎需要制表符分隔的 robocopy 日志,无论如何对列定义都有好处。
- Datagridview 需要很长时间才能显示 30MB 的数据,所以我使用了
过滤器
- 我从网上借用代码创建了一个 Schema.ini,如下所述
(所以错误:代码不会再从 Visual Studio 粘贴。代码工具会翻转到 Java 的其他网页。)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Cursor = Cursors.WaitCursor
'http://ss64.com/nt/robocopy.html can suppress header and summary
Dim sFile As String = "c:\temp\robo.txt" ' seems to need a .txt or .csv, .log didn't work
CreateRoboLogSchema(sFile) ' recreates each pass, no needed once things work
Dim sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & IO.Path.GetDirectoryName(sFile) & ";Extended Properties='text';"
' use Schema.ini for: HDR=Yes;FMT=TabDelimited' and column definitions
Dim dt As New DataTable()
Dim SQL As String = "SELECT * FROM " & IO.Path.GetFileName(sFile)
'SQL &= " WHERE State <> 'Same'"
Using adapt As New OleDbDataAdapter(SQL, sConn)
adapt.Fill(dt)
End Using
Debug.Print("|" & dt.Rows(0)(1) & "|") ' show import trimmed leading spaces (trims trailing too)
' DGV slow to load large files, use filter to display target rows
Dim dv As New DataView(dt)
dv.RowFilter = "State <> 'Same'" ' not case sensitive
DataGridView1.DataSource = dv
DataGridView1.Columns(0).Visible = False
DataGridView1.AutoResizeColumns()
Catch ex As Exception
MsgBox(ex.Message)
Finally
'Cursor=Cursors.Default
End Try
End Sub
Private Function CreateRoboLogSchema(ByVal strFileName As String) As Boolean
' edit http://www.vb-tips.com/CSVDataSet.aspx
Dim ascii As System.Text.Encoding = System.Text.Encoding.ASCII
Dim swSchema As System.IO.StreamWriter = Nothing
Dim blnReturn As Boolean
Dim strSchemaPath As String = System.IO.Path.GetFileName(strFileName)
Try
strSchemaPath = IO.Path.GetDirectoryName(strFileName) & "\Schema.ini"
swSchema = My.Computer.FileSystem.OpenTextFileWriter(strSchemaPath, False, ascii)
Dim strFile As String = System.IO.Path.GetFileName(strFileName)
swSchema.WriteLine("[" & IO.Path.GetFileName(strFileName) & "]")
swSchema.WriteLine("ColNameHeader=False")
swSchema.WriteLine("Format=TabDelimited")
swSchema.WriteLine("Col1=Value1 Text") ' file specific
swSchema.WriteLine("Col2=State Text")
swSchema.WriteLine("Col3=DirChanges Text")
swSchema.WriteLine("Col4=Size Text")
swSchema.WriteLine("Col5=Filepath Text")
'Continue for all fields
blnReturn = True
Catch ex As Exception
blnReturn = False
Finally
If swSchema IsNot Nothing Then
swSchema.Close()
End If
End Try
Return blnReturn
End Function
(我不需要 OleDbDataAdapter 的替代品。)
下面的代码可以找到并读取文件,但 DGV 有四列(如预期的那样),但所有数据行的第一列只有文本。
Dim sDir As String = "c:\temp\"
Dim sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sDir & ";Extended Properties='text;HDR=Yes;FMT=TabDelimited';"
Dim dt As New DataTable()
Using adapt As New OleDbDataAdapter(String.Format("SELECT TOP 100 * FROM robo.txt"), sConn)
adapt.Fill(dt)
End Using
DataGridView1.DataSource = dt
我认为扩展属性是唯一的要求。我尝试添加 Schema.ini 无济于事 - 我认为它甚至没有被读取,因为列 header 与模式不匹配。
最成功的传递中的 header 行使用逗号作为分隔符 - 这导致四列具有正确的名称,但制表符将所有数据分隔在 Col1 中。如果我在 header 行中使用制表符,我会得到一些系统分配列 (3),它对应于带有两个逗号的数据行。
我做错了什么?
这是制表符被替换为 <tab>
的前几行。从那以后,我注意到我在数据中多了一列。对下方 header 行的修复并未解决问题 - 所有数据都转储到第一个字段中。
在 header 中使用制表符分隔符而不是逗号,结果所有 header 文本和数据都被转储到第一个字段。
col1,state,col3,size,path
<tab> same<tab><tab> 102912<tab>\APCD04T\Data\Thumbs.db
<tab> same<tab><tab> 22016<tab>\APCD04T\Data\APCD Topical Info\APCD_Boards&Committees_List.doc
<tab> same<tab><tab> 4.3 m<tab>\APCD04T\Data\APCD Topical Info\LOSSAN-LAtoSLORailCorridorStrategicPlan.pdf
在尝试使用 OLEDB 将 RoboCopy
日志加载到 DataTable
时学到了一些东西。
- 日志文件需要有 .txt 或 .csv(或?)扩展名,.log 失败。
- Schema.ini 似乎需要制表符分隔的 robocopy 日志,无论如何对列定义都有好处。
- Datagridview 需要很长时间才能显示 30MB 的数据,所以我使用了 过滤器
- 我从网上借用代码创建了一个 Schema.ini,如下所述
(所以错误:代码不会再从 Visual Studio 粘贴。代码工具会翻转到 Java 的其他网页。)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Cursor = Cursors.WaitCursor
'http://ss64.com/nt/robocopy.html can suppress header and summary
Dim sFile As String = "c:\temp\robo.txt" ' seems to need a .txt or .csv, .log didn't work
CreateRoboLogSchema(sFile) ' recreates each pass, no needed once things work
Dim sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & IO.Path.GetDirectoryName(sFile) & ";Extended Properties='text';"
' use Schema.ini for: HDR=Yes;FMT=TabDelimited' and column definitions
Dim dt As New DataTable()
Dim SQL As String = "SELECT * FROM " & IO.Path.GetFileName(sFile)
'SQL &= " WHERE State <> 'Same'"
Using adapt As New OleDbDataAdapter(SQL, sConn)
adapt.Fill(dt)
End Using
Debug.Print("|" & dt.Rows(0)(1) & "|") ' show import trimmed leading spaces (trims trailing too)
' DGV slow to load large files, use filter to display target rows
Dim dv As New DataView(dt)
dv.RowFilter = "State <> 'Same'" ' not case sensitive
DataGridView1.DataSource = dv
DataGridView1.Columns(0).Visible = False
DataGridView1.AutoResizeColumns()
Catch ex As Exception
MsgBox(ex.Message)
Finally
'Cursor=Cursors.Default
End Try
End Sub
Private Function CreateRoboLogSchema(ByVal strFileName As String) As Boolean
' edit http://www.vb-tips.com/CSVDataSet.aspx
Dim ascii As System.Text.Encoding = System.Text.Encoding.ASCII
Dim swSchema As System.IO.StreamWriter = Nothing
Dim blnReturn As Boolean
Dim strSchemaPath As String = System.IO.Path.GetFileName(strFileName)
Try
strSchemaPath = IO.Path.GetDirectoryName(strFileName) & "\Schema.ini"
swSchema = My.Computer.FileSystem.OpenTextFileWriter(strSchemaPath, False, ascii)
Dim strFile As String = System.IO.Path.GetFileName(strFileName)
swSchema.WriteLine("[" & IO.Path.GetFileName(strFileName) & "]")
swSchema.WriteLine("ColNameHeader=False")
swSchema.WriteLine("Format=TabDelimited")
swSchema.WriteLine("Col1=Value1 Text") ' file specific
swSchema.WriteLine("Col2=State Text")
swSchema.WriteLine("Col3=DirChanges Text")
swSchema.WriteLine("Col4=Size Text")
swSchema.WriteLine("Col5=Filepath Text")
'Continue for all fields
blnReturn = True
Catch ex As Exception
blnReturn = False
Finally
If swSchema IsNot Nothing Then
swSchema.Close()
End If
End Try
Return blnReturn
End Function