Files
ADMIN/VERAGMonitoring/AtlasAufschubDatenEinlesen.vb
2019-08-08 12:44:50 +02:00

143 lines
6.0 KiB
VB.net

Imports System.Data.SqlClient
Imports System.IO
Module AtlasAufschubDatenEinlesen
Public Sub initAllFiles(path As String) 'Alle Files im Ordner durchlaufen
For Each file As String In IO.Directory.GetFiles(path)
initFile(file)
Next
insertSQL()
'frmMain
End Sub
Sub writeLineToSqlInsert(brgak_datum, brgak_betrag, brgak_brgaktoId, brgak_filename, brgak_atc)
Try
Using sr As New StreamWriter(AppDomain.CurrentDomain.BaseDirectory & "sql.txt", True)
sr.WriteLine(brgak_datum & ";" & brgak_betrag & ";" & brgak_brgaktoId & ";" & brgak_filename & ";" & brgak_atc)
End Using
Catch ex As Exception
writeLog("ERROR", "Fehler beim Schreiben der SQL Datei: " & ex.Message)
End Try
End Sub
Public Sub insertSQL()
If System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory & "sql.txt") Then
Try
Using sr As New StreamReader(AppDomain.CurrentDomain.BaseDirectory & "sql.txt")
Dim line As String = ""
Do While sr.Peek() >= 0
line = sr.ReadLine
If line.Contains(";") Then
Dim s = line.Split(";")
tryToInsertProgramm(s(0), CDbl(s(1)), s(2), s(3), s(4))
End If
Loop
End Using
My.Computer.FileSystem.WriteAllText(AppDomain.CurrentDomain.BaseDirectory & "sql.txt", "", False)
Catch ex As Exception
writeLog("ERROR", "Fehler beim Lesen der SQL Datei: " & ex.Message)
End Try
End If
End Sub
Public Sub initFile(file As String)
Dim checkfile As Boolean = False
Try
Using sr As New StreamReader(file)
If Not file.ToLower.EndsWith("tmp") Then
Dim t As DateTime = Now
Do
checkfile = FileInUse(file)
Loop Until checkfile = True Or t.AddSeconds(3) < Now
Dim line As String = ""
Dim rowcnt = 1
Do While sr.Peek() >= 0
line = CStr(sr.ReadLine())
Dim s = line.Split(Chr(29)) 'nach GroupSeperator splitten
If s(0) = "ASK" AndAlso s(7) = "006128" Then
Dim filename = cut_file(file)
Dim betrag As Double = CDbl(s(12).Substring(0, 9) & "," & s(12).Substring(9, 2))
' MsgBox(betrag)
Dim fi As New System.IO.FileInfo(file)
writeLineToSqlInsert(fi.CreationTime.ToString("dd.MM.yyyy hh:mm:ss"), betrag, "1", filename, s(1))
End If
rowcnt += 1
Loop
' writeLogVerarbeitet(cut_file(file))
End If
End Using
Catch ex As Exception
writeLog("ERROR", "Fehler beim Initialisieren der ATLAS-Datei: " & ex.Message)
End Try
insertSQL()
End Sub
Private Function cut_file(ByVal file As String) As String ' Funktion zum Entfernen der Backslashs / Ordner While file.Contains("\") file = file.Remove(0, 1) End While Return file End Function
While file.Contains("\")
file = file.Remove(0, 1)
End While
Return file
End Function
Public Function FileInUse(ByVal sFile As String) As Boolean
If System.IO.File.Exists(sFile) Then
Try
Dim F As Short = FreeFile()
FileOpen(F, sFile, IO.FileMode.Open, OpenAccess.Read, OpenShare.LockRead)
FileClose(F)
Catch
Return True
End Try
End If
Return False
End Function
Public Function tryToInsertProgramm(brgak_datum, brgak_betrag, brgak_brgaktoId, brgak_filename, brgak_atc) As Boolean
Dim sql As String = " begin tran" &
" if Not exists (select * from tblBrgAufschub with (updlock,serializable) where brgak_filename = @brgak_filename) " &
" begin " &
" INSERT INTO tblBrgAufschub " &
" (brgak_datum, brgak_betrag, brgak_brgaktoId, brgak_filename, brgak_atc) VALUES " &
" (@brgak_datum, @brgak_betrag, @brgak_brgaktoId, @brgak_filename, @brgak_atc) " &
" End " &
" commit tran "
Dim cn As New SqlConnection()
cn.ConnectionString = "Data Source=BUCHHALTUNG\SQLEXPRESS;Initial Catalog=ADMIN;Integrated Security=false;User ID=sa;Password=BmWr501956;"
cn.Open()
Using cn
Using cmd As New SqlCommand(sql, cn)
cmd.Parameters.AddWithValue("@brgak_datum", brgak_datum)
cmd.Parameters.AddWithValue("@brgak_betrag", brgak_betrag)
cmd.Parameters.AddWithValue("@brgak_brgaktoId", brgak_brgaktoId)
cmd.Parameters.AddWithValue("@brgak_filename", brgak_filename)
cmd.Parameters.AddWithValue("@brgak_atc", brgak_atc)
Try
cmd.ExecuteNonQuery()
Return True
Catch ex As SqlException
writeLog("ERROR", "Fehler beim Schreiben in die Datenbank: " & ex.Message)
End Try
End Using
End Using
Return False
End Function
Public Sub writeLog(typ, msg)
' If Not System.IO.Directory.Exists(AppDomain.CurrentDomain.BaseDirectory & "log\") Then
'System.IO.Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory & "log\")
'End If
' Try
' Using sr As New StreamWriter(AppDomain.CurrentDomain.BaseDirectory & "log\err.log", True)
MsgBox(typ & ";" & msg)
' End Using
' Catch ex As Exception
' End Try
End Sub
End Module