Offline Modus Verbesserungen, SMB Share für User FIX
This commit is contained in:
@@ -15,6 +15,95 @@ Public Class cSQL
|
||||
con.Close()
|
||||
End Sub
|
||||
|
||||
Sub UpdateDatenbankStruktur()
|
||||
Dim conn As New SqlConnection(Class1.DBDoku)
|
||||
Dim cmd As New SqlCommand("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'", conn)
|
||||
conn.Open()
|
||||
Dim reader As SqlDataReader = cmd.ExecuteReader()
|
||||
|
||||
While reader.Read()
|
||||
Dim tableName As String = reader("TABLE_NAME").ToString()
|
||||
Console.WriteLine("CREATE TABLE " & tableName)
|
||||
' Hier kannst du dann den CREATE TABLE Befehl generieren
|
||||
UpdateOrCreateTable(Class1.DBDoku, Class1.lokalDBDoku, tableName)
|
||||
SyncDatenbank(tableName)
|
||||
End While
|
||||
conn.Close()
|
||||
|
||||
End Sub
|
||||
Sub SyncDatenbank(tabelle As String)
|
||||
Dim conn As New SqlConnection(Class1.lokalDBDoku)
|
||||
Dim cmd As New SqlCommand($"
|
||||
DELETE FROM {tabelle};
|
||||
INSERT INTO {tabelle}
|
||||
SELECT * FROM [sqlcluster.verag.ost.dmn].doku.dbo.{tabelle};", conn)
|
||||
|
||||
Try
|
||||
conn.Open()
|
||||
cmd.ExecuteNonQuery()
|
||||
conn.Close()
|
||||
Console.WriteLine("Datenbank erfolgreich synchronisiert!")
|
||||
Catch ex As Exception
|
||||
Console.WriteLine("Fehler bei der Synchronisation: " & ex.Message)
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
Sub UpdateOrCreateTable(sqlServerConnectionString As String, localSqlServerConnectionString As String, tableName As String)
|
||||
' SQL Server-Abfrage für die Spalteninformationen
|
||||
Dim query As String = $"SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{tableName}'"
|
||||
Dim columnDefinitions As New List(Of String)
|
||||
|
||||
' Abfrage für die Spaltenstruktur der Remote-Tabelle
|
||||
Using con As New SqlConnection(sqlServerConnectionString)
|
||||
con.Open()
|
||||
Using cmd As New SqlCommand(query, con)
|
||||
Using reader As SqlDataReader = cmd.ExecuteReader()
|
||||
While reader.Read()
|
||||
Dim columnName As String = reader("COLUMN_NAME").ToString()
|
||||
Dim dataType As String = reader("DATA_TYPE").ToString()
|
||||
If reader("CHARACTER_MAXIMUM_LENGTH") IsNot DBNull.Value Then
|
||||
dataType &= $"({reader("CHARACTER_MAXIMUM_LENGTH").ToString()})"
|
||||
If dataType = "varchar(-1)" Then dataType = "varchar(max)"
|
||||
End If
|
||||
Dim isNullable As String = If(reader("IS_NULLABLE").ToString() = "YES", "NULL", "NOT NULL")
|
||||
columnDefinitions.Add($"{columnName} {dataType} {isNullable}")
|
||||
End While
|
||||
End Using
|
||||
End Using
|
||||
End Using
|
||||
|
||||
' Die lokale SQL Server-Datenbank, in der die Tabelle erstellt wird
|
||||
Using localSqlCon As New SqlConnection(localSqlServerConnectionString)
|
||||
localSqlCon.Open()
|
||||
|
||||
' Prüfen, ob die Tabelle bereits existiert
|
||||
Dim tableExistsQuery As String = $"SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{tableName}'"
|
||||
Using cmd As New SqlCommand(tableExistsQuery, localSqlCon)
|
||||
Dim exists As Object = cmd.ExecuteScalar()
|
||||
If exists Is Nothing Then
|
||||
' Erstelle die Tabelle, wenn sie nicht existiert
|
||||
Dim createTableSQL As String = $"CREATE TABLE {tableName} ({String.Join(", ", columnDefinitions)});"
|
||||
Using createCmd As New SqlCommand(createTableSQL, localSqlCon)
|
||||
createCmd.ExecuteNonQuery()
|
||||
End Using
|
||||
Else
|
||||
' Falls die Tabelle bereits existiert, überprüfe und füge neue Spalten hinzu
|
||||
For Each columnDef In columnDefinitions
|
||||
Dim columnName As String = columnDef.Split(" ")(0)
|
||||
Dim addColumnSQL As String = $"ALTER TABLE {tableName} ADD COLUMN {columnDef};"
|
||||
Try
|
||||
Using alterCmd As New SqlCommand(addColumnSQL, localSqlCon)
|
||||
alterCmd.ExecuteNonQuery()
|
||||
End Using
|
||||
Catch ex As Exception
|
||||
' Falls die Spalte bereits existiert, wird die Exception ignoriert
|
||||
End Try
|
||||
Next
|
||||
End If
|
||||
End Using
|
||||
End Using
|
||||
End Sub
|
||||
|
||||
Public Shared Sub UpdateSQL(ByRef table As String, ByRef values As String, ByRef where As String, Optional ByRef DBConnect As String = "")
|
||||
If DBConnect = "" Then DBConnect = Class1.DBConString
|
||||
Dim con As New SqlConnection
|
||||
|
||||
Reference in New Issue
Block a user