Imports OracleInProcServer

Public Class frmDatasetOO4O
    Inherits System.Windows.Forms.Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は dispose をオーバーライドしてコンポーネント一覧を消去します。
    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

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    ' Windows フォーム デザイナを使って変更してください。  
    ' コード エディタは使用しないでください。
    Friend WithEvents tmrLoad As System.Windows.Forms.Timer
    Friend WithEvents cmdUpdate As System.Windows.Forms.Button
    Friend WithEvents cmdQuery As System.Windows.Forms.Button
    Friend WithEvents lblSQL As System.Windows.Forms.Label
    Friend WithEvents dgrdDataset As System.Windows.Forms.DataGrid
    Friend WithEvents dsOracle As System.Data.DataSet
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.tmrLoad = New System.Windows.Forms.Timer(Me.components)
        Me.cmdUpdate = New System.Windows.Forms.Button()
        Me.cmdQuery = New System.Windows.Forms.Button()
        Me.lblSQL = New System.Windows.Forms.Label()
        Me.dgrdDataset = New System.Windows.Forms.DataGrid()
        Me.dsOracle = New System.Data.DataSet()
        CType(Me.dgrdDataset, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.dsOracle, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'tmrLoad
        '
        Me.tmrLoad.Interval = 1
        '
        'cmdUpdate
        '
        Me.cmdUpdate.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right)
        Me.cmdUpdate.Location = New System.Drawing.Point(312, 256)
        Me.cmdUpdate.Name = "cmdUpdate"
        Me.cmdUpdate.Size = New System.Drawing.Size(72, 24)
        Me.cmdUpdate.TabIndex = 15
        Me.cmdUpdate.Text = "&Update"
        '
        'cmdQuery
        '
        Me.cmdQuery.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
        Me.cmdQuery.Location = New System.Drawing.Point(312, 4)
        Me.cmdQuery.Name = "cmdQuery"
        Me.cmdQuery.Size = New System.Drawing.Size(72, 24)
        Me.cmdQuery.TabIndex = 1
        Me.cmdQuery.Text = "&Query"
        '
        'lblSQL
        '
        Me.lblSQL.Location = New System.Drawing.Point(8, 4)
        Me.lblSQL.Name = "lblSQL"
        Me.lblSQL.Size = New System.Drawing.Size(296, 22)
        Me.lblSQL.TabIndex = 0
        Me.lblSQL.Text = "SELECT * FROM SCOTT.EMP"
        Me.lblSQL.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'dgrdDataset
        '
        Me.dgrdDataset.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.dgrdDataset.DataMember = ""
        Me.dgrdDataset.DataSource = Me.dsOracle
        Me.dgrdDataset.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dgrdDataset.Location = New System.Drawing.Point(0, 40)
        Me.dgrdDataset.Name = "dgrdDataset"
        Me.dgrdDataset.Size = New System.Drawing.Size(392, 208)
        Me.dgrdDataset.TabIndex = 16
        '
        'dsOracle
        '
        Me.dsOracle.DataSetName = "NewDataSet"
        Me.dsOracle.Locale = New System.Globalization.CultureInfo("ja-JP")
        '
        'frmDatasetOO4O
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(392, 294)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.dgrdDataset, Me.cmdUpdate, Me.cmdQuery, Me.lblSQL})
        Me.Name = "frmDatasetOO4O"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "OO4O + Dataset Read/Write"
        CType(Me.dgrdDataset, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.dsOracle, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub frmDatasetOO4O_Load(ByVal sender As System.Object, _
                                   ByVal e As System.EventArgs) Handles MyBase.Load
        Me.cmdUpdate.Enabled = False
        tmrLoad.Enabled = True
    End Sub

    Private Sub tmrLoad_Tick(ByVal sender As System.Object, _
                             ByVal e As System.EventArgs) Handles tmrLoad.Tick
        Dim frmLogon As Form = New frmLogonOO4O()
        Dim intRet As MsgBoxResult

        tmrLoad.Enabled = False
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor()
        intRet = frmLogon.ShowDialog()
        frmLogon.Dispose()
        frmLogon = Nothing
        Me.Cursor = System.Windows.Forms.Cursors.Default
        If intRet = MsgBoxResult.Cancel Then
            Me.Close()
        End If
    End Sub

    Private Sub cmdQuery_Click(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) Handles cmdQuery.Click
        Dim oraDs As OraDynaset
        Dim strSQL As String = Me.lblSQL.Text.Trim
        Dim strErrText As String
        Dim tblsRst As Data.DataTableCollection = Me.dsOracle.Tables
        Dim rowRst As Data.DataRow
        Dim iintCol As Integer

        Try
            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor()
            Me.cmdUpdate.Enabled = False
            '
            Do While tblsRst.Count > 0
                tblsRst.RemoveAt(tblsRst.Count - 1)
            Loop
            oraDs = p_oraDb.CreateDynaset(strSQL, _
                                          dynOption.ORADYN_READONLY)
            If Not oraDs.EOF Then
                tblsRst.Add()
                For iintCol = 0 To oraDs.Fields.Count - 1
                    tblsRst(0).Columns.Add(oraDs.Fields(iintCol).Name)
                Next
                Me.dgrdDataset.DataSource = tblsRst(0)
            End If
            Do While Not oraDs.EOF
                rowRst = tblsRst(0).NewRow
                For iintCol = 0 To oraDs.Fields.Count - 1
                    rowRst(iintCol) = oraDs.Fields(iintCol).Value
                Next
                tblsRst(0).Rows.Add(rowRst)
                oraDs.MoveNext()
            Loop
            tblsRst(0).AcceptChanges()
            Me.dgrdDataset.Refresh()
            Me.cmdUpdate.Enabled = True
        Catch
            strErrText = ErrorToString()
            MsgBox(strErrText, _
                   MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, _
                   Me.Text)
        Finally
            If Not oraDs Is Nothing Then
                oraDs = Nothing
            End If
            Me.Cursor = System.Windows.Forms.Cursors.Default
        End Try
    End Sub

    Private Sub cmdUpdate_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) Handles cmdUpdate.Click
        Dim oraDs As OraDynaset
        Dim strSQL As String
        Dim strErrText As String
        Dim iintRow As Integer
        Dim iintCol As Integer
        Dim tblsRst As Data.DataTableCollection = Me.dsOracle.Tables
        Dim rowRst As Data.DataRow

        Try
            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor()
            p_oraDb.Parameters.Add("EMPNO", 1, 1)
            '
            For Each rowRst In tblsRst(0).Rows
                Select Case rowRst.RowState
                    Case DataRowState.Unchanged
                    Case DataRowState.Added, DataRowState.Modified
                        strSQL = Me.lblSQL.Text.Trim & " WHERE EMPNO=:EMPNO"
                        p_oraDb.Parameters("EMPNO").Value = rowRst(0)
                        oraDs = p_oraDb.CreateDynaset(strSQL, _
                                                      dynOption.ORADYN_ORAMODE)
                        If Not oraDs.EOF Then
                            oraDs.Edit()
                        Else
                            oraDs.AddNew()
                            oraDs.Fields("EMPNO").value = rowRst(0)
                        End If
                        For iintCol = 1 To oraDs.Fields.Count - 1
                            oraDs.Fields(iintCol).value = rowRst(iintCol)
                        Next
                        oraDs.Update()
                    Case DataRowState.Deleted
                        rowRst.RejectChanges()
                        strSQL = "DELETE FROM EMP WHERE EMPNO=:EMPNO"
                        p_oraDb.Parameters("EMPNO").Value = rowRst(0)
                        p_oraDb.ExecuteSQL(strSQL)
                        rowRst.Delete()
                End Select
            Next
            tblsRst(0).AcceptChanges()
        Catch
            strErrText = ErrorToString()
            MsgBox(strErrText, _
                   MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, _
                   Me.Text)
        Finally
            p_oraDb.Parameters.Remove("EMPNO")
            If Not oraDs Is Nothing Then
                oraDs = Nothing
            End If
            Me.Cursor = System.Windows.Forms.Cursors.Default
        End Try
    End Sub
End Class
