Files
Lehrlingsparcours/_archiv/LP/.svn/pristine/c6/c67c2622d2eaa5143415c78d507222e48c96aecf.svn-base
2019-12-21 10:58:30 +01:00

107 lines
4.5 KiB
Plaintext

Imports System.Data.SQLite
Public Class frmdbcopy
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.OpenFileDialog1.DefaultExt = "*.db"
Me.OpenFileDialog1.Filter = "SQLite-Datenbanken (*.db)|*.db|Alle Dateien (*.*)|*.*"
Me.OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName <> "" Then
Me.TextBox1.Text = Me.OpenFileDialog1.FileName
get_Tables()
End If
Me.TextBox1.Text = OpenFileDialog1.FileName
End Sub
Private Function get_Tables()
Dim selectFirst As Boolean = False
Dim SQLconnect As New SQLite.SQLiteConnection()
Dim SQLcommand As SQLiteCommand
Dim dbconnection As String = "data source=" + Me.TextBox1.Text & ";"
SQLconnect.ConnectionString = dbconnection
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
Dim SchemaTable = SQLconnect.GetSchema(SQLiteMetaDataCollectionNames.Tables)
Me.ListBox1.Items.Clear()
For int As Integer = 0 To SchemaTable.Rows.Count - 1
If SchemaTable.Rows(int)!TABLE_TYPE.ToString = "table" Then
Me.ListBox1.Items.Add(SchemaTable.Rows(int)!TABLE_NAME.ToString())
selectFirst = True
End If
Next
SQLcommand.Dispose()
SQLconnect.Close()
Me.ListBox1.Enabled = True
End Function
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Me.Button2.Enabled = True
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If MsgBox("Daten werden gelöscht aus:" + vbCrLf + "- Datenbank: " + Globals.Datenbank + vbCrLf + "- Tabelle: " + Me.ListBox1.SelectedItem + vbCrLf + vbCrLf + " und mit den Daten ersetzt von:" + vbCrLf + "- Datenbank: " + Me.TextBox1.Text + vbCrLf + "- Tabelle " + Me.ListBox1.SelectedItem + vbCrLf + vbCrLf + " Sind Sie sicher?", vbYesNo + vbQuestion) = vbYes Then
delete_Target()
copy_Data()
End If
End Sub
Private Sub Delete_Target()
Try
Dim SQLconnect As New SQLite.SQLiteConnection()
Dim SQLcommand As SQLiteCommand
Dim dbconnection As String = Globals.Datenbank
SQLconnect.ConnectionString = dbconnection
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandType = CommandType.Text
SQLcommand.CommandText = "Delete from " + Me.ListBox1.SelectedItem
SQLcommand.ExecuteNonQuery()
SQLcommand.Dispose()
SQLconnect.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Copy_Data()
Dim ds As New DataSet
Dim ds1 As New DataSet
Try
Dim SQLconnect As New SQLite.SQLiteConnection()
SQLconnect.ConnectionString = "data source=" + Me.TextBox1.Text & ";"
SQLconnect.Open()
Dim da As New SQLiteDataAdapter("", SQLconnect)
Dim sqlcmd As New SQLiteCommand
sqlcmd.Connection = SQLconnect
sqlcmd.CommandType = CommandType.Text
da.SelectCommand.CommandText = "Select * from " + Me.ListBox1.SelectedItem
da.Fill(ds, "Daten")
SQLconnect.Close()
SQLconnect.ConnectionString = Globals.Datenbank
SQLconnect.Open()
Dim da1 As New SQLiteDataAdapter("", SQLconnect)
sqlcmd.Connection = SQLconnect
sqlcmd.CommandType = CommandType.Text
da1.SelectCommand.CommandText = "Select * from " + Me.ListBox1.SelectedItem
da1.Fill(ds1, "Daten")
Dim myBuilder As New SQLite.SQLiteCommandBuilder(da1)
For Each dr As DataRow In ds.Tables(0).Rows
Dim ndr As DataRow
ndr = ds1.Tables(0).NewRow
Dim i As Integer = 0
For i = 0 To ds1.Tables(0).Columns.Count - 1
ndr.Item(i) = dr.Item(i)
Next
ds1.Tables(0).Rows.Add(ndr)
Next
da1.Update(ds1, "Daten")
SQLconnect.Close()
MsgBox("Es wurden " + ds1.Tables(0).Rows.Count.ToString + " Datensätze kopiert.", vbInformation)
Me.Button2.Enabled = False
Me.ListBox1.SelectedItem = ""
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class