kn8
This commit is contained in:
@@ -4,6 +4,7 @@ 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
|
||||
@@ -91,4 +92,114 @@ Public Class cTariffKN8_Interface
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user