206 lines
8.8 KiB
VB.net
206 lines
8.8 KiB
VB.net
|
|
Imports System.IO
|
|
Imports System.Globalization
|
|
Imports System.Net
|
|
Imports io.konik.zugferd.entity
|
|
Imports System.Data.SqlClient
|
|
Imports System.Windows.Forms
|
|
|
|
' Definieren der Klasse, um die Daten zu repräsentieren
|
|
Public Class cTariffKN8
|
|
Public Property Spalte1 As String
|
|
Public Property Spalte2 As String
|
|
Public Property Spalte3 As String
|
|
Public Property Spalte4 As String
|
|
End Class
|
|
Public Class cTariffKN8_Interface
|
|
|
|
|
|
|
|
Public Shared Function getKN8FromTNR(TNR)
|
|
If TNR Is Nothing Then Return ""
|
|
If TNR Is DBNull.Value Then Return ""
|
|
If TNR.ToString = String.Empty Then Return ""
|
|
TNR = TNR.ToString.Replace(".", "")
|
|
TNR = TNR.ToString.Replace(" ", "")
|
|
If TNR.ToString.Length > 8 Then TNR = TNR.ToString.Substring(0, 8)
|
|
|
|
'If TNR.Length < 8 Then
|
|
' ' Mit Nullen auffüllen
|
|
' TNR = TNR.PadRight(8, "0"c)
|
|
'End If
|
|
Dim sql As New VERAG_PROG_ALLGEMEIN.SQL
|
|
Return sql.getValueTxtBySql("SELECT TOP 1 [Kurztitel] FROM [tblEZTKN8] where EDV_Code like '" & TNR & "'", "FMZOLL",,, "")
|
|
End Function
|
|
|
|
|
|
Public Shared Function UPDATE_KN8()
|
|
' URL zur Datei
|
|
Dim fileUrl As String = "https://www.statistik.at/kdb/downloads/csv/prod/KN" & Now.Year & "_DE_CKT.txt"
|
|
|
|
'Dim fileUrl As String = "https://www.statistik.at/kdb/downloads/csv/prod/KN2024_DE_CKT.txt"
|
|
|
|
Try
|
|
' WebClient verwenden, um die Datei direkt herunterzuladen
|
|
Using client As New WebClient()
|
|
Dim fileContent As String = client.DownloadString(fileUrl)
|
|
|
|
' Datei-Inhalt in Zeilen aufteilen
|
|
Dim lines As String() = fileContent.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
|
|
|
|
' Verbindung zur Datenbank öffnen
|
|
Using connection As New SqlConnection(VERAG_PROG_ALLGEMEIN.SQL.GetFMZOLLConnectionString)
|
|
connection.Open()
|
|
|
|
Using command As New SqlCommand("DELETE tblEZTKN8 ", connection)
|
|
|
|
command.ExecuteNonQuery()
|
|
|
|
End Using
|
|
|
|
' Zeilen durchlaufen, Kopfzeile überspringen
|
|
For i As Integer = 1 To lines.Length - 1
|
|
Dim line As String = lines(i).Trim()
|
|
|
|
' Überspringen leerer Zeilen
|
|
If String.IsNullOrEmpty(line) Then Continue For
|
|
|
|
' Spalten trennen (Semikolon als Trennzeichen)
|
|
Dim columns As String() = line.Split(";")
|
|
|
|
' Sicherstellen, dass genügend Spalten vorhanden sind
|
|
If columns.Length >= 4 Then
|
|
' Daten in die Datenbank einfügen
|
|
Using command As New SqlCommand("INSERT INTO tblEZTKN8 (Ebene, EDV_Code, Code, Kurztitel) VALUES (@Spalte1, @Spalte2, @Spalte3, @Spalte4)", connection)
|
|
command.Parameters.AddWithValue("@Spalte1", columns(0).Trim().Replace("""", ""))
|
|
command.Parameters.AddWithValue("@Spalte2", columns(1).Trim().Replace("""", ""))
|
|
command.Parameters.AddWithValue("@Spalte3", columns(2).Trim().Replace("""", ""))
|
|
command.Parameters.AddWithValue("@Spalte4", columns(3).Trim().Replace("""", ""))
|
|
|
|
command.ExecuteNonQuery()
|
|
|
|
End Using
|
|
End If
|
|
Next
|
|
End Using
|
|
End Using
|
|
Return True
|
|
|
|
Console.WriteLine("Daten erfolgreich in die Datenbank geladen.")
|
|
Catch ex As Exception
|
|
VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
|
|
End Try
|
|
Return False
|
|
End Function
|
|
Public Shared Function UPDATE_KN8_DESTAT()
|
|
' URL zur Datei
|
|
Dim fileUrl As String = "https://www.destatis.de/DE/Methoden/Klassifikationen/Aussenhandel/sova-leitdatei.html"
|
|
'--> Manuell laden
|
|
|
|
Try
|
|
Dim openFileDialog As New OpenFileDialog()
|
|
openFileDialog.Title = "Wähle eine Textdatei"
|
|
openFileDialog.Filter = "Textdateien (*.txt)|*.txt|Alle Dateien (*.*)|*.*"
|
|
|
|
If openFileDialog.ShowDialog() = DialogResult.OK Then
|
|
Dim filePath As String = openFileDialog.FileName
|
|
If File.Exists(filePath) Then
|
|
Dim lines() As String = File.ReadAllLines(filePath)
|
|
|
|
' Verbindung zur Datenbank öffnen
|
|
Using connection As New SqlConnection(VERAG_PROG_ALLGEMEIN.SQL.GetFMZOLLConnectionString)
|
|
connection.Open()
|
|
|
|
Using command As New SqlCommand("DELETE tblEZTKN8_DE ", connection)
|
|
|
|
command.ExecuteNonQuery()
|
|
|
|
End Using
|
|
|
|
|
|
For Each line As String In lines
|
|
' Sicherstellen, dass die Zeile nicht leer ist
|
|
If Not String.IsNullOrWhiteSpace(line) Then
|
|
' Annahme: Der Code besteht aus den ersten 8 Zeichen, danach folgt die Beschreibung
|
|
Dim code As String = line.Substring(0, 8).Trim()
|
|
Dim description As String = line.Substring(9).Trim()
|
|
|
|
|
|
Using command As New SqlCommand("INSERT INTO tblEZTKN8_DE (code, description) VALUES (@code, @description )", connection)
|
|
command.Parameters.AddWithValue("@code", code.Trim().Replace("""", ""))
|
|
command.Parameters.AddWithValue("@description", description.Trim().Replace("""", ""))
|
|
|
|
command.ExecuteNonQuery()
|
|
|
|
End Using
|
|
|
|
|
|
' Entferne unnötige Leerzeichen aus der Beschreibung
|
|
description = System.Text.RegularExpressions.Regex.Replace(description, "\s{2,}", " ")
|
|
|
|
Console.WriteLine($"Code: {code}, Beschreibung: {description}")
|
|
End If
|
|
Next
|
|
|
|
End Using
|
|
Else
|
|
Console.WriteLine("Datei nicht gefunden!")
|
|
End If
|
|
End If
|
|
|
|
|
|
|
|
|
|
' WebClient verwenden, um die Datei direkt herunterzuladen
|
|
Using client As New WebClient()
|
|
Dim fileContent As String = client.DownloadString(fileUrl)
|
|
|
|
' Datei-Inhalt in Zeilen aufteilen
|
|
Dim lines As String() = fileContent.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
|
|
|
|
' Verbindung zur Datenbank öffnen
|
|
Using connection As New SqlConnection(VERAG_PROG_ALLGEMEIN.SQL.GetFMZOLLConnectionString)
|
|
connection.Open()
|
|
|
|
Using command As New SqlCommand("DELETE tblEZTKN8_DE ", connection)
|
|
|
|
command.ExecuteNonQuery()
|
|
|
|
End Using
|
|
|
|
' Zeilen durchlaufen, Kopfzeile überspringen
|
|
For i As Integer = 1 To lines.Length - 1
|
|
Dim line As String = lines(i).Trim()
|
|
|
|
' Überspringen leerer Zeilen
|
|
If String.IsNullOrEmpty(line) Then Continue For
|
|
|
|
' Spalten trennen (Semikolon als Trennzeichen)
|
|
Dim columns As String() = line.Split(";")
|
|
|
|
' Sicherstellen, dass genügend Spalten vorhanden sind
|
|
If columns.Length >= 4 Then
|
|
' Daten in die Datenbank einfügen
|
|
Using command As New SqlCommand("INSERT INTO tblEZTKN8_DE (Ebene, EDV_Code, Code, Kurztitel) VALUES (@Spalte1, @Spalte2, @Spalte3, @Spalte4)", connection)
|
|
command.Parameters.AddWithValue("@Spalte1", columns(0).Trim().Replace("""", ""))
|
|
command.Parameters.AddWithValue("@Spalte2", columns(1).Trim().Replace("""", ""))
|
|
command.Parameters.AddWithValue("@Spalte3", columns(2).Trim().Replace("""", ""))
|
|
command.Parameters.AddWithValue("@Spalte4", columns(3).Trim().Replace("""", ""))
|
|
|
|
command.ExecuteNonQuery()
|
|
|
|
End Using
|
|
End If
|
|
Next
|
|
End Using
|
|
End Using
|
|
Return True
|
|
|
|
Console.WriteLine("Daten erfolgreich in die Datenbank geladen.")
|
|
Catch ex As Exception
|
|
VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
|
|
End Try
|
|
Return False
|
|
End Function
|
|
End Class
|