Imports MySql.Data  ' 데이터베이스 연동을 위한 네임스페이스 추가
Imports MySql.Data.MySqlClient  ' MYSQL Server용 네임스페이스 추가
Module MySqlModule
    Private Const ServerIP As String = "192.168.0.250"
    Private Const DataBaseName As String = "jomtTesterDB"
    Private Const UserName As String = "root"
    Private Const Password As String = "JOMT1646db!!"
    Private Const Conn As String = "Server=" + ServerIP + ";DataBase=" + DataBaseName + ";user=" + UserName + ";password=" + Password + ";"

    Private sqlCon As New MySqlConnection(Conn)
    Private sqlCmd As New MySqlCommand()
    Private sqlData As MySqlDataReader
    Public sqlDataQueryScalar As Object
    Public sqlDataQuery() As Object

    Private Function DBOpen() As Boolean
        Try
            If sqlCon.State <> ConnectionState.Open Then
                sqlCon.Open()
            End If
            Return True
        Catch ex As Exception
            MsgBox("MySqlCommand Error:" + vbCrLf + Err.Description)
            Return False
        End Try
    End Function

    Private Function DBClose() As Boolean
        Try
            If sqlCon.State = ConnectionState.Open Then
                sqlCon.Close()
            End If
            Return True
        Catch ex As Exception
            MsgBox("MySqlCommand Error:" + vbCrLf + Err.Description)
            Return False
        End Try
    End Function
    Public Function DBCommand(cmdStr As String) As Boolean
        Try
            sqlCon.Open()
            sqlCmd.CommandText = "USE jomtTesterDB;"
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery()

            sqlCmd.CommandText = cmdStr
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery()

            sqlCon.Close()

            Return True
        Catch ex As Exception
            MsgBox("MySqlCommand Error:" + vbCrLf + Err.Description)
            sqlCon.Close()
            Return False
        End Try
        End
    End Function

    Public Function DBQueryScalar(cmdStr As String) As String
        Try
            sqlCon.Open()
            sqlCmd.CommandText = "USE jomtTesterDB;"
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery()

            sqlCmd.CommandText = cmdStr
            sqlCmd.Connection = sqlCon
            sqlDataQueryScalar = sqlCmd.ExecuteScalar()

            sqlCon.Close()

            Return sqlDataQueryScalar
        Catch ex As Exception
            MsgBox("MySqlCommand Error:" + vbCrLf + Err.Description)
            sqlCon.Close()
            Return False
        End Try
        End
    End Function

    Public Function DBQueryReader(cmdStr As String) As Integer
        Dim FieldCount As Integer
        Dim QueryCount As ULong
        Dim initRun As Boolean = True

        Try
            DBOpen()

            sqlCmd.CommandText = "USE jomtTesterDB;"
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery()

            sqlCmd.CommandText = cmdStr
            sqlCmd.Connection = sqlCon
            sqlData = sqlCmd.ExecuteReader

            FieldCount = sqlData.FieldCount

            While (sqlData.Read())
                If initRun Then
                    ReDim sqlDataQuery(FieldCount - 1)
                    initRun = False
                Else
                    ReDim Preserve sqlDataQuery(UBound(sqlDataQuery) + FieldCount)
                End If

                For cnt = 0 To FieldCount - 1
                    sqlDataQuery(QueryCount) = sqlData.GetString(cnt)
                    QueryCount += 1
                Next
            End While

            DBClose()
            Return QueryCount

        Catch ex As Exception
            'MsgBox("MySqlCommand Error:" + vbCrLf + Err.Description)
            If sqlCon.State = ConnectionState.Open Then sqlCon.Close()
            Return 0
        End Try
        End
    End Function

End Module
