129 lines
5.9 KiB
VB.net
129 lines
5.9 KiB
VB.net
Imports System.Data.SqlClient
|
|
Imports System.Data.SQLite
|
|
|
|
Public Class cSQL2SQLLite
|
|
|
|
|
|
Function Main(tableName As String)
|
|
|
|
Dim sqlServerConString = Class1.DBDoku
|
|
Dim sqliteFile As String = "C:\verag\offline_db.sqlite"
|
|
Dim sqliteConnString As String = $"Data Source={sqliteFile};Version=3;"
|
|
|
|
|
|
|
|
UpdateOrCreateTable(sqlServerConString, sqliteConnString, tableName)
|
|
CopyDataIncrementally(sqlServerConString, sqliteConnString, tableName)
|
|
|
|
End Function
|
|
|
|
|
|
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()})"
|
|
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
|
|
|
|
|
|
Sub CopyDataIncrementally(sqlServerConnectionString As String, sqliteConnectionString As String, tableName As String)
|
|
Dim query As String = $"SELECT * FROM {tableName}"
|
|
|
|
Using sqlCon As New SqlConnection(sqlServerConnectionString)
|
|
sqlCon.Open()
|
|
Using cmd As New SqlCommand(query, sqlCon)
|
|
Using reader As SqlDataReader = cmd.ExecuteReader()
|
|
Using sqliteCon As New SQLiteConnection(sqliteConnectionString)
|
|
sqliteCon.Open()
|
|
|
|
While reader.Read()
|
|
Dim columns As New List(Of String)
|
|
Dim values As New List(Of String)
|
|
Dim parameters As New List(Of SQLiteParameter)
|
|
|
|
For i As Integer = 0 To reader.FieldCount - 1
|
|
Dim columnName As String = reader.GetName(i)
|
|
columns.Add(columnName)
|
|
values.Add($"@{columnName}")
|
|
parameters.Add(New SQLiteParameter($"@{columnName}", reader.GetValue(i)))
|
|
Next
|
|
|
|
Dim insertSQL As String = $"INSERT OR IGNORE INTO {tableName} ({String.Join(", ", columns)}) VALUES ({String.Join(", ", values)})"
|
|
Using insertCmd As New SQLiteCommand(insertSQL, sqliteCon)
|
|
insertCmd.Parameters.AddRange(parameters.ToArray())
|
|
insertCmd.ExecuteNonQuery()
|
|
End Using
|
|
End While
|
|
End Using
|
|
End Using
|
|
End Using
|
|
End Using
|
|
End Sub
|
|
|
|
Function MapSQLServerToSQLiteType(sqlType As String, length As Object) As String
|
|
Select Case sqlType.ToLower()
|
|
Case "int", "bigint", "smallint", "tinyint"
|
|
Return "INTEGER"
|
|
Case "bit"
|
|
Return "BOOLEAN"
|
|
Case "decimal", "numeric", "money", "smallmoney", "float", "real"
|
|
Return "REAL"
|
|
Case "char", "nchar", "varchar", "nvarchar", "text", "ntext"
|
|
Return "TEXT"
|
|
Case "datetime", "smalldatetime", "date", "time", "datetime2"
|
|
Return "TEXT"
|
|
Case Else
|
|
Return "TEXT"
|
|
End Select
|
|
End Function
|
|
|
|
End Class
|