Imports System
Imports System.ComponentModel
Imports System.Collections
Imports System.Diagnostics
Imports System.Data.OleDb
Imports C1.Data

' C1SchemaDef container class: main class of the data library.
' Contains the C1SchemaDef component managing the data schema. 
Public Class DataClass
    Inherits System.ComponentModel.Component

#Region " Component Designer generated code "

    ' C1SchemaDef component managing the data schema.
    ' Needs to be public in order to allow access to the library.
    Public WithEvents SchemaDef1 As C1.Data.C1SchemaDef

    Public Sub New(Container As System.ComponentModel.IContainer)
        MyClass.New()

        'Required for Windows.Forms Class Composition Designer support
        Container.Add(me)
    End Sub

    Public Sub New()
        MyBase.New()
        'This call is required by the Component Designer.
        InitializeComponent()
    End Sub

    'Component overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    Friend WithEvents C1DataSetLogic1 As C1.Data.C1DataSetLogic
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(DataClass))
        Me.SchemaDef1 = New C1.Data.C1SchemaDef
        Me.C1DataSetLogic1 = New C1.Data.C1DataSetLogic
        CType(Me.SchemaDef1, System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'SchemaDef1
        '
        Me.SchemaDef1.DataObjectsAssemblyFlags = CType((C1.Data.DataObjectsAssemblyFlags.DataSetNamespaces Or C1.Data.DataObjectsAssemblyFlags.StaticNameFields), C1.Data.DataObjectsAssemblyFlags)
        Me.SchemaDef1.DummyImage = CType(resources.GetObject("SchemaDef1.DummyImage"), System.Drawing.Bitmap)
        Me.SchemaDef1.SerializedSchemaXML = Me.SchemaDef1.GetSchemaFromResources(resources, "SchemaDef1", Me)
        Me.SchemaDef1.SerializedDesignTimeOptionsXML = Me.SchemaDef1.GetDesignTimeOptionsFromResources(resources, "SchemaDef1", Me)
        '
        'C1DataSetLogic1
        '
        Me.C1DataSetLogic1.DataSetDef = "DataSet"
        Me.C1DataSetLogic1.SchemaComponent = Me.SchemaDef1
        Me.C1DataSetLogic1.SchemaPersist = Nothing
        CType(Me.SchemaDef1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

#End Region

    ' This code demonstrates how you can set Connection.DbConnection/DbTransaction directly to
    ' a pre-created connection/transaction object. If you set Connection.DbConnection before
    ' C1DataObjects opens the connection to fetch data, C1DataObjects does not create a
    ' new connection object, uses the one you supplied. Similarly, C1DataObjects opens a new
    ' transaction only if you did not set Connection.DbTransaction.
    Private Sub C1DataSetLogic1_BeforeFetch(ByVal sender As Object, ByVal e As C1.Data.FetchEventArgs) Handles C1DataSetLogic1.BeforeFetch
        If e.TableView Is Nothing Then
            Dim conn As OleDbConnection = GetConnection()
            conn.Open()
            SchemaDef1.Schema.Connections(0).DbConnection = conn
        End If
    End Sub

    Private Sub C1DataSetLogic1_AfterFetch(ByVal sender As Object, ByVal e As C1.Data.FetchEventArgs) Handles C1DataSetLogic1.AfterFetch
        If e.TableView Is Nothing Then
            SchemaDef1.Schema.Connections(0).DbConnection.Close()
        End If
    End Sub

    Private Sub C1DataSetLogic1_BeforeUpdate(ByVal sender As Object, ByVal e As C1.Data.DataSetEventArgs) Handles C1DataSetLogic1.BeforeUpdate
        Dim conn As OleDbConnection = GetConnection()
        conn.Open()
        SchemaDef1.Schema.Connections(0).DbConnection = conn
        SchemaDef1.Schema.Connections(0).DbTransaction = conn.BeginTransaction()
    End Sub

    Private Sub C1DataSetLogic1_AfterUpdate(ByVal sender As Object, ByVal e As C1.Data.DataSetEventArgs) Handles C1DataSetLogic1.AfterUpdate
        SchemaDef1.Schema.Connections(0).DbTransaction.Commit()
        SchemaDef1.Schema.Connections(0).DbConnection.Close()
    End Sub

    Private Function GetConnection() As OleDbConnection
        Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\ComponentOne Studio.NET 2.0\common\Nwind.mdb;Persist Security Info=False")

        ' Switch to C1NWind.mdb database
        Dim dbPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + _
            "\ComponentOne Samples\Common\C1NWind.mdb"
        If System.IO.File.Exists(dbPath) Then
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath
        End If

        Return conn
    End Function

#Region " Switch to C1NWind.mdb database "

    Private Sub SchemaDef1_CreateSchema(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SchemaDef1.CreateSchema
        Dim dbPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + _
            "\ComponentOne Samples\Common\C1NWind.mdb"
        If System.IO.File.Exists(dbPath) Then
            Dim conn As C1.Data.SchemaObjects.Connection = SchemaDef1.Schema.Connections(0)
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath
        End If
    End Sub

#End Region

End Class
