我们有一个小的.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
我们现在遇到一个问题,因为时钟机格式已经改变,在我们不需要的文件前面包含一些额外的信息。
Office%5197,04 / 06 / 2013,22:08:54,2
直到并包含%的第一个元素是固定宽度,并始终具有%。
在代码中是否有一个简单的删除内容,所以我们只捕获5197,04 / 06 / 2013,22:08:54,2例如,或者它是重写,因为我不是开发者,只是继承了这个问题。
欢呼声
约翰
答案 0 :(得分: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
答案 1 :(得分: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