'Imports System.Data.OleDb
'Imports System.Reflection
Imports System.ComponentModel
Imports Excel = Microsoft.Office.Interop.Excel
''Imports Microsoft.Office.Interop


Public Class frmMySqlTest
    Private cmd As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        '''DataGridView를 선택하면 전체 한줄이 선택되게 됩니다. 
        DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect

        '컬럼 2개를 생성하고 각각의 이름을 정해줍니다. DataGridView1의 상단에 위치하게 됩니다. 
        DataGridView.ColumnCount = 7
        DataGridView.Columns(0).Name = "번호"
        DataGridView.Columns(1).Name = "시리얼번호"
        DataGridView.Columns(2).Name = "주파수"
        DataGridView.Columns(3).Name = "임피던스"
        DataGridView.Columns(4).Name = "캐패시턴스"
        DataGridView.Columns(5).Name = "레지스턴스"
        DataGridView.Columns(6).Name = "비고"


    End Sub

    Private Sub btnReadDB_Click(sender As Object, e As EventArgs) Handles btnReadDB.Click
        txbPV_Date.Clear()
        txbPV_Freq.Clear()
        txbPV_Z.Clear()
        txbPV_C.Clear()
        txbPV_R.Clear()

        cmd = "select PV_Date,PV_Frequency,PV_Impedance,PV_Capacitance,PV_Resistance from jomtCartridgeTbl where PV_SN = '" + txbPV_SN.Text + "';"
        If DBQueryReader(cmd) Then
            txbPV_Date.Text = Mid(sqlDataQuery(0), 1, InStr(sqlDataQuery(0), " "))
            txbPV_Freq.Text = sqlDataQuery(1)
            txbPV_Z.Text = sqlDataQuery(2)
            txbPV_C.Text = sqlDataQuery(3)
            txbPV_R.Text = sqlDataQuery(4)
        Else
            txbPV_Date.Text = "N/A"
            txbPV_Freq.Text = "N/A"
            txbPV_Z.Text = "N/A"
            txbPV_C.Text = "N/A"
            txbPV_R.Text = "N/A"
        End If


        ''sqlData = sqlCmd.ExecuteReader
        ''If sqlData.Read() Then
        ''    txbPV_Date.Text = sqlData.GetString(0)
        ''    txbPV_Freq.Text = sqlData.GetString(1)
        ''    txbPV_Z.Text = sqlData.GetString(2)
        ''    txbPV_C.Text = sqlData.GetString(3)
        ''    txbPV_R.Text = sqlData.GetString(4)
        ''Else
        ''    txbPV_Date.Text = "N/A"
        ''    txbPV_Freq.Text = "N/A"
        ''    txbPV_Z.Text = "N/A"
        ''    txbPV_C.Text = "N/A"
        ''    txbPV_R.Text = "N/A"
        ''End If
    End Sub

    Private Sub btnUpdateDB_Click(sender As Object, e As EventArgs) Handles btnUpdateDB.Click

        cmd = "update jomtCartridgeTbl set PV_Date = '" + txbPV_Date.Text + "',PV_Frequency = " + txbPV_Freq.Text + ",PV_Impedance = " + txbPV_Z.Text +
              ", PV_Capacitance = " + txbPV_C.Text + ", PV_Resistance = " + txbPV_R.Text + " where PV_SN = '" + txbPV_SN.Text + "';"
        DBCommand(cmd)

        txbPV_Freq.Clear()
        txbPV_Z.Clear()
        txbPV_C.Clear()
        txbPV_R.Clear()


    End Sub

    Private Sub btnInsrtDB_Click(sender As Object, e As EventArgs) Handles btnInsrtDB.Click


        cmd = "use jomtTesterDB;"
        DBCommand(cmd)

        cmd = "insert jomtCartridgeTbl(TestStep,PV_SN,PV_Date,PV_Frequency,PV_Impedance,PV_Capacitance,PV_Resistance) 
               values('1','" + txbPV_SN.Text + "','" + txbPV_Date.Text + "','" + txbPV_Freq.Text + "','" + txbPV_Z.Text + "','" + txbPV_C.Text + "','" + txbPV_R.Text + "');"
        DBCommand(cmd)


    End Sub

    Private Sub btnDeleteDB_Click(sender As Object, e As EventArgs) Handles btnDeleteDB.Click

        Try

            If (MessageBox.Show("정말 삭제하시겠습니까?", "시리얼 번호 삭제", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
                cmd = "delete from jomtCartridgeTbl where PV_SN = '" + txbPV_SN.Text + "';"
                DBCommand(cmd)

                MessageBox.Show("삭제가 완료되었습니다.", "삭제 완료")

            Else
                MessageBox.Show("취소 되었습니다.", "삭제 취소")

            End If

        Catch ex As Exception
            MsgBox("Error:" + vbCrLf + Err.Description)
        End Try

    End Sub

    'Keep the application object and the workbook object global, so you can  
    'retrieve the data in Button2_Click that was set in Button1_Click.
    Dim objApp As Excel.Application
    Dim objBook As Excel._Workbook

    Private Sub DisplayQuarterlySales(oWS As Excel.Worksheet)
        Dim oResizeRange As Excel.Range
        Dim oChart As Excel.Chart
        Dim iNumQtrs As Integer
        Dim sMsg As String
        Dim iRet As Integer

        ' Determine how many quarters to display data for.
        For iNumQtrs = 4 To 2 Step -1
            sMsg = "Enter sales data for" & Str(iNumQtrs) & " quarter(s)?"
            iRet = MsgBox(sMsg, vbYesNo Or vbQuestion _
         Or vbMsgBoxSetForeground, "Quarterly Sales")
            If iRet = vbYes Then Exit For
        Next iNumQtrs

        sMsg = "Displaying data for" & Str(iNumQtrs) & " quarter(s)."
        MsgBox(sMsg, vbMsgBoxSetForeground, "Quarterly Sales")

        ' Starting at E1, fill headers for the number of columns selected.
        oResizeRange = oWS.Range("E1", "E1").Resize(ColumnSize:=iNumQtrs)

        oResizeRange.Formula = "=""Q"" & COLUMN()-4 & CHAR(10) & ""Sales"""

        ' Change the Orientation and WrapText properties for the headers.
        oResizeRange.Orientation = 38
        oResizeRange.WrapText = True

        ' Fill the interior color of the headers.
        oResizeRange.Interior.ColorIndex = 36

        ' Fill the columns with a formula and apply a number format.
        oResizeRange = oWS.Range("E2", "E6").Resize(ColumnSize:=iNumQtrs)
        oResizeRange.Formula = "=RAND()*100"
        oResizeRange.NumberFormat = "$0.00"

        ' Apply borders to the Sales data and headers.
        oResizeRange = oWS.Range("E1", "E6").Resize(ColumnSize:=iNumQtrs)
        oResizeRange.Borders.Weight = Excel.XlBorderWeight.xlThin

        ' Add a Totals formula for the sales data and apply a border.
        oResizeRange = oWS.Range("E8", "E8").Resize(ColumnSize:=iNumQtrs)
        oResizeRange.Formula = "=SUM(E2:E6)"
        With oResizeRange.Borders(Excel.XlBordersIndex.xlEdgeBottom)
            .LineStyle = Excel.XlLineStyle.xlDouble
            .Weight = Excel.XlBorderWeight.xlThick

        End With

        ' Add a Chart for the selected data
        oResizeRange = oWS.Range("E2:E6").Resize(ColumnSize:=iNumQtrs)
        oChart = oWS.Parent.Charts.Add
        With oChart
            .ChartWizard(oResizeRange, Excel.XlChartType.xl3DColumn, , Excel.XlChartType.xl3DColumn)
            .SeriesCollection(1).XValues = oWS.Range("A2", "A6")
            For iRet = 1 To iNumQtrs
                .SeriesCollection(iRet).Name = "=""Q" & Str(iRet) & """"
            Next iRet
            .Location(Excel.XlLocationInTable.xlColumnHeader, oWS.Name)
        End With

        ' Move the chart so as not to cover your data.
        With oWS.Shapes("Chart 1")
            .Top = oWS.Rows(10).Top
            .Left = oWS.Columns(2).Left

        End With

        ' Free any references.
        oChart = Nothing
        oResizeRange = Nothing

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        loadExcelDB()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim TestStep, PV_Srial, PV_Date, PV_Freq, PV_Z, PV_C, PV_R, PV_Test As String
        Dim Overlap As Integer

        Try
            TestStep = "1"
            PV_Date = txbPVdate.Text

            For cnt = DataGridView.Rows.Count - 2 To 0 Step -1
                PV_Srial = DataGridView.Rows(cnt).Cells(1).Value.ToString
                PV_Freq = DataGridView.Rows(cnt).Cells(2).Value.ToString
                PV_Z = DataGridView.Rows(cnt).Cells(3).Value.ToString
                PV_C = DataGridView.Rows(cnt).Cells(4).Value.ToString
                PV_R = DataGridView.Rows(cnt).Cells(5).Value.ToString

                cmd = "select count(PV_SN) from jomtCartridgeTbl where PV_SN = '" + PV_Srial + "';"

                DBQueryReader(cmd)

                PV_Test = sqlDataQuery(0)

                If (PV_Test = "0") Then
                    cmd = "insert jomtCartridgeTbl(TestStep,PV_Date, PV_SN,PV_Frequency,PV_Impedance,PV_Capacitance,PV_Resistance) value('" + TestStep + "','" + PV_Date + "','" + PV_Srial + "','" + PV_Freq + "','" + PV_Z + "','" + PV_C + "','" + PV_R + "');"
                    DBCommand(cmd)
                    Me.DataGridView.Rows.RemoveAt(cnt)

                Else
                    Overlap += 1
                End If

            Next

            If (Overlap > 0) Then
                MsgBox("중복 시리얼 넘버가 " & Overlap & " 개 존재합니다.")
            Else
                MsgBox("트렌스듀서 등록이 완료 되었습니다.")
            End If

        Catch ex As Exception
            MsgBox("Error:" + vbCrLf + Err.Description)
        End Try

    End Sub

    Private Sub DataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView.CellClick

        Try

            If DataGridView.Rows(e.RowIndex).Cells(1).Value Is Nothing Then

                txbPV_SN.Text = "null"
                txbPV_Date.Text = "null"
                txbPV_Freq.Text = "null"
                txbPV_Z.Text = "null"
                txbPV_C.Text = "null"
                txbPV_R.Text = "null"

            Else
                txbPV_SN.Text = DataGridView.Rows(e.RowIndex).Cells(1).Value.ToString()
                txbPV_Date.Text = txbPVdate.Text.ToString
                txbPV_Freq.Text = DataGridView.Rows(e.RowIndex).Cells(2).Value.ToString()
                txbPV_Z.Text = DataGridView.Rows(e.RowIndex).Cells(3).Value.ToString()
                txbPV_C.Text = DataGridView.Rows(e.RowIndex).Cells(4).Value.ToString()
                txbPV_R.Text = DataGridView.Rows(e.RowIndex).Cells(5).Value.ToString()
            End If

        Catch ex As Exception
            MsgBox("Error:" + vbCrLf + Err.Description)
        End Try

    End Sub

End Class
