0

我们有一个小型 .net vb 应用程序,它读取由我们的机器打卡创建的文本文件。

然后将此信息存储到 SQL 数据库中。

以前的数据线是这样的(下图)

5208,05/06/2013,06:27:18,1

这是一个 ID、日期、时间和时钟值。

将文本拆分并为 sql 做好准备的代码如下所示:

            For Each fileInfo As FileInfo In allFiles
                If fileInfo.Length > 0 Then
                    If fileInfo.LastWriteTime <> Now Then
                        System.Threading.Thread.Sleep(500)
                        Dim srReader As New StreamReader(fileInfo.FullName.ToString)
                        strSplit = Split(srReader.ReadToEnd(), Chr(13))
                        srReader.Close()
                        srReader.Dispose()
                        srReader = Nothing
                        For intInner As Integer = 0 To strSplit.Length - 1
                            strInnerSplit = Split(strSplit(intInner), ",")
                            If strInnerSplit.Length > 3 Then
                                Dim strCard As Integer = strInnerSplit(0)
                                Dim intType As Integer = strInnerSplit(3)
                                Dim dteInOut As Date = strInnerSplit(2)
                                Dim dteDate As Date = strInnerSplit(1)

                                If alreadyInsertedWithinSQL(strCard, dteDate & " " & dteInOut, intType) = False Then
                                    importWithinSQL(strCard, dteDate & " " & dteInOut, intType)
                                End If
                            End If
                        Next
                    End If
                End If
            Next
        End If

我们现在遇到了一个问题,因为时钟机器格式发生了变化,在文件前面包含了一些我们不需要的额外信息。

办公室 %5197,04/06/2013,22:08:54,2

直到并包括 % 的第一个元素是固定宽度,并且始终具有 %。

是否有一个简单的方法可以在代码中删除它,以便我们只捕获 5197,04/06/2013,22:08:54,2 例如,还是重写,因为我不是开发人员,只是继承了这个问题。

干杯

约翰

4

2 回答 2

0

也许像这样...

声明字符串 var ..Dim s as String

                        For intInner As Integer = 0 To strSplit.Length - 1
                            s=Split(strSplit(intInner), "%")(1)
                            strInnerSplit = Split(s, ",")
                            If strInnerSplit.Length > 3 Then
                                Dim strCard As Integer = strInnerSplit(0)
                                Dim intType As Integer = strInnerSplit(3)
                                Dim dteInOut As Date = strInnerSplit(2)
                                Dim dteDate As Date = strInnerSplit(1)

                                If alreadyInsertedWithinSQL(strCard, dteDate & " " & dteInOut, intType) = False Then
                                    importWithinSQL(strCard, dteDate & " " & dteInOut, intType)
                                End If
                            End If
                        Next
于 2013-06-05T10:00:41.600 回答
0

像这样的东西应该工作:

                    For intInner As Integer = 0 To strSplit.Length - 1
                        strInnerSplit = Split(strSplit(intInner), ",")
                        If strInnerSplit.Length > 3 Then
                            'Use the split method on the % and use the second
                            'element which is index 1
                            Dim strCard As Integer = strInnerSplit(0).Split("%")(1)
                            Dim intType As Integer = strInnerSplit(3)
                            Dim dteInOut As Date = strInnerSplit(2)
                            Dim dteDate As Date = strInnerSplit(1)

                            If alreadyInsertedWithinSQL(strCard, dteDate & " " & dteInOut, intType) = False Then
                                importWithinSQL(strCard, dteDate & " " & dteInOut, intType)
                            End If
                        End If
                    Next
于 2013-06-05T06:02:46.720 回答