Hi guys,
Im Pretty New to Developing on Net and have started in VB Net 2010. After Many searches looking for a sample of Connecting and Running Data Queries etc. and coming up short, I have made the following Class in VB Net 2010. I know its not fantastic But it should be able to give some scope on how to do things.
Please any feedback and Improvements would be greatly appreciated!
Code:
Imports MySql.Data.MySqlClient
Public Class clsMySql
#Region "IDisposable Object ..."
Implements IDisposable
Private disposedValue As Boolean = False
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free unmanaged resources when explicitly called
End If
' TODO: free shared unmanaged resources
Call sDisConnect()
End If
Me.disposedValue = True
End Sub
#End Region
#Region "Events Declaration"
Event eErrorOccured(ByVal stMessage As String)
Event eExceptionOccured(ByVal exErr As Exception)
#End Region
#Region "Class Properties"
#Region "Basic Class Properties"
Private mIsReaderBusy As Boolean = False
Public ReadOnly Property pIsReaderBusy() As Boolean
Get
Return mIsReaderBusy
End Get
End Property
Private mLastError As String
Public ReadOnly Property pLastError() As String
Get
Return mLastError
End Get
End Property
Private mLastExeption As Exception
Public ReadOnly Property pLastExeption() As Exception
Get
Return mLastExeption
End Get
End Property
Private WithEvents mMySqlConnection As MySqlConnection
Public ReadOnly Property pMySqlConnection() As MySqlConnection
Get
Return mMySqlConnection
End Get
End Property
Private mMySqlConnectionString As String
Public Property pMySqlConnectionString() As String
Get
Return mMySqlConnectionString
End Get
Set(ByVal value As String)
mMySqlConnectionString = value
End Set
End Property
#End Region
#Region "Advanced Class Properties"
Private mToolStripMenuItem As ToolStripMenuItem
Public Sub pSetToolStripMenuItem(ByRef inToolStripMenuItem As ToolStripMenuItem)
mToolStripMenuItem = inToolStripMenuItem
End Sub
Public Function pGetToolStripMenuItem()
Return mToolStripMenuItem
End Function
Private mToolStripStatusLabel As ToolStripStatusLabel
Public Sub pSetToolStripStatusLabel(ByRef inToolStripStatusLabel As ToolStripStatusLabel)
mToolStripStatusLabel = inToolStripStatusLabel
End Sub
Public Function pGetToolStripStatusLabel()
Return mToolStripStatusLabel
End Function
Public ReadOnly Property pDatabase() As String
Get
Return mMySqlConnection.Database
End Get
End Property
#End Region
#End Region
#Region "Connection Object Event Handlers"
Private Sub mMySqlConnection_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles mMySqlConnection.Disposed
Try
If (Not mToolStripStatusLabel Is Nothing) Then
mToolStripStatusLabel.ToolTipText = mToolStripStatusLabel.Tag & " - " & "Not Available"
mToolStripStatusLabel.Image = My.Resources.Resources.p16MySQL
End If
If (Not mToolStripMenuItem Is Nothing) Then
mToolStripMenuItem.Checked = False
End If
Catch exErr As Exception
sGenerateException(exErr)
End Try
End Sub
Private Sub mMySqlConnection_StateChange(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs) Handles mMySqlConnection.StateChange
Dim stToolTip As String
Dim imgStatus As Image = My.Resources.Resources.p16MySQL
Dim chkMenu As Boolean = False
Try
Select Case e.CurrentState
Case ConnectionState.Open
stToolTip = "Connected"
imgStatus = My.Resources.Resources.p16MySQL2
chkMenu = True
Case ConnectionState.Closed
stToolTip = "Closed"
Case ConnectionState.Broken
stToolTip = "Broken"
Case ConnectionState.Connecting
stToolTip = "Connecting"
imgStatus = My.Resources.Resources.p16MySQL3
chkMenu = True
Case ConnectionState.Executing
stToolTip = "Executing"
imgStatus = My.Resources.Resources.p16MySQL3
chkMenu = True
Case ConnectionState.Fetching
stToolTip = "Fetching"
imgStatus = My.Resources.Resources.p16MySQL3
chkMenu = True
Case Else
stToolTip = "Unknown State"
End Select
If (Not mToolStripStatusLabel Is Nothing) Then
mToolStripStatusLabel.Tag = mMySqlConnection.Database
mToolStripStatusLabel.Text = ""
stToolTip = mToolStripStatusLabel.Tag & " - " & stToolTip
mToolStripStatusLabel.ToolTipText = stToolTip
mToolStripStatusLabel.Image = imgStatus
End If
If (Not mToolStripMenuItem Is Nothing) Then
mToolStripMenuItem.Checked = chkMenu
End If
Catch exErr As Exception
sGenerateException(exErr)
End Try
End Sub
#End Region
#Region "Output Events Methods"
Private Sub sGenerateError(ByVal stMessage As String)
mLastError = stMessage
RaiseEvent eErrorOccured(stMessage)
End Sub
Private Sub sGenerateException(ByVal exErr As Exception, _
Optional ByVal bIncludeErr As Boolean = False, _
Optional ByVal stMessage As String = "")
mLastExeption = exErr
RaiseEvent eExceptionOccured(exErr)
If bIncludeErr = True Then
RaiseEvent eErrorOccured(stMessage)
End If
End Sub
#End Region
#Region "Basic Class Methods"
Public Sub New()
End Sub
Public Sub New(ByVal stConnectionString As String)
mMySqlConnectionString = stConnectionString
End Sub
Public Sub New(ByVal stConnectionString As String, _
ByRef inToolStripMenuItem As ToolStripMenuItem)
mMySqlConnectionString = stConnectionString
mToolStripMenuItem = inToolStripMenuItem
End Sub
Public Sub New(ByVal stConnectionString As String, _
ByRef inToolStripMenuItem As ToolStripMenuItem, _
ByRef inToolStripStatusLabel As ToolStripStatusLabel)
mMySqlConnectionString = stConnectionString
mToolStripMenuItem = inToolStripMenuItem
mToolStripStatusLabel = inToolStripStatusLabel
End Sub
#End Region
#Region "Advanced Class Methods"
Public Sub sConnect()
Dim bReturn As Boolean = fConnect()
If bReturn = False Then
sGenerateError("Failed to Connect to Database")
End If
End Sub
Public Sub sConnect(ByVal stConnectionstring As String)
mMySqlConnectionString = stConnectionstring
fConnect()
End Sub
Public Sub sConnect(ByVal stConnectionString As String, _
ByRef inToolStripMenuItem As ToolStripMenuItem)
mToolStripMenuItem = inToolStripMenuItem
sConnect(stConnectionString)
End Sub
Public Sub sConnect(ByVal stConnectionString As String, _
ByRef inToolStripMenuItem As ToolStripMenuItem, _
ByRef inToolStripStatusLabel As ToolStripStatusLabel)
mToolStripStatusLabel = inToolStripStatusLabel
sConnect(stConnectionString, inToolStripMenuItem)
End Sub
Public Sub sDisConnect()
Dim bReturn As Boolean = fDisConnect()
If bReturn = False Then
sGenerateError("Failed to DisConnect from Database")
End If
End Sub
Public Sub sVerify()
Dim bReturn As Boolean = fVerify()
If bReturn = False Then
sGenerateError("Failed to Verify Database")
End If
End Sub
Public Sub sVerify(ByVal bReconnect As Boolean)
Dim bReturn As Boolean = fVerify(bReconnect)
If bReturn = False Then
sGenerateError("Failed to Verify Database")
End If
End Sub
Public Sub sMySQLDataReader_Close(ByRef MySQLRDR As MySqlDataReader)
Try
'Close and Dispose Datareader object
If Not MySQLRDR Is Nothing Then
If MySQLRDR.IsClosed = False Then
MySQLRDR.Close()
End If
MySQLRDR.Dispose()
MySQLRDR = Nothing
End If
'Reset Datareader Marker
mIsReaderBusy = False
Catch exErr As Exception
sGenerateException(exErr)
End Try
End Sub
#End Region
#Region "Advanced Class Functions"
Public Function fConnect() As Boolean
Dim bReturn As Boolean = False
Try
'Close Exisiting Connection
If (Not mMySqlConnection Is Nothing) Then
'Close any Exisiting Connections
If mMySqlConnection.State = ConnectionState.Open Then
Call sDisConnect()
End If
End If
'Create New Connection
mMySqlConnection = New MySqlConnection
With mMySqlConnection
.ConnectionString = mMySqlConnectionString
.Open()
End With
bReturn = True
Catch exErr As Exception
sGenerateException(exErr)
End Try
Return bReturn
End Function
Public Function fConnect(ByVal stConnectionstring As String) As Boolean
mMySqlConnectionString = stConnectionstring
Return fConnect()
End Function
Public Function fConnect(ByVal stConnectionString As String, _
ByRef inToolStripMenuItem As ToolStripMenuItem) As Boolean
mToolStripMenuItem = inToolStripMenuItem
Return fConnect(stConnectionString)
End Function
Public Function fConnect(ByVal stConnectionString As String, _
ByRef inToolStripMenuItem As ToolStripMenuItem, _
ByRef inToolStripStatusLabel As ToolStripStatusLabel) As Boolean
mToolStripStatusLabel = inToolStripStatusLabel
Return fConnect(stConnectionString, inToolStripMenuItem)
End Function
Public Function fDisConnect() As Boolean
Dim bReturn As Boolean = False
Try
'Clear Object Variables
If (Not mMySqlConnection Is Nothing) Then
If mMySqlConnection.State <> ConnectionState.Closed Then
mMySqlConnection.Close()
End If
mMySqlConnection.Dispose()
mMySqlConnection = Nothing
End If
bReturn = True
Catch exErr As Exception
sGenerateException(exErr)
End Try
Return bReturn
End Function
Public Function fVerify() As Boolean
Dim bReturn As Boolean = False
Try
If (Not mMySqlConnection Is Nothing) Then
If (mMySqlConnection.State = ConnectionState.Open) Then
Using MySQLCMD As New MySqlCommand("Show Tables", mMySqlConnection)
'Set Data Reader Busy Variable
MySQLCMD.CommandType = CommandType.Text
Dim MySQLRDR As MySqlDataReader
Dim iaSync As IAsyncResult
'If Data Reader is Busy Then Connection is Verified
If mIsReaderBusy = False Then
mIsReaderBusy = True
iaSync = MySQLCMD.BeginExecuteReader(CommandBehavior.SingleRow)
MySQLRDR = MySQLCMD.EndExecuteReader(iaSync)
Do Until iaSync.IsCompleted = True
'Wait until aSync Reader Finished
Loop
'Close Reader as we have finished with it
sMySQLDataReader_Close(MySQLRDR)
End If
End Using
bReturn = True
End If
End If
Catch exErr As Exception
sGenerateException(exErr)
Finally
'Disconnect if Failed Verify
If bReturn = False Then
sDisConnect()
End If
End Try
Return bReturn
End Function
Public Function fVerify(ByVal bReconnect As Boolean) As Boolean
Dim bReturn As Boolean = fVerify()
If bReturn = False And bReconnect = True Then
bReturn = fConnect()
End If
Return bReturn
End Function
#End Region
#Region "Advanced Data Methods"
#End Region
#Region "Advanced Data Functions"
Public Function fRowArrayFromReader(ByRef ReturnArray() As Object, _
ByVal stSQLStatement As String) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
Dim OutArray() As Object = Nothing
Dim mySqlRDR As MySqlDataReader = Nothing
ReturnArray = Nothing
bReturn = fMySqlDataReader(mySqlRDR, stSQLStatement, CommandBehavior.SingleResult)
If bReturn > 0 And mySqlRDR.HasRows Then
bReturn = 0
With mySqlRDR
While .Read
'Load Array Line By Line From Data Reader
ReDim OutArray(.FieldCount - 1)
.GetValues(OutArray)
bReturn += 1
End While
End With
End If
'Close Data Reader
sMySQLDataReader_Close(mySqlRDR)
'Send Default output Variable anbd Buffer
ReturnArray = OutArray
Return bReturn
End Function
Public Function fDataTable(ByRef ReturnDT As DataTable, _
ByVal stSQLStatement As String) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
ReturnDT.Clear()
'Start Using a New DataAdapter with New MySqlCommand
Using MySQLDA As New MySqlDataAdapter()
MySQLDA.SelectCommand = New MySqlCommand(stSQLStatement, mMySqlConnection)
'Fill DataTable From MySql Adapter
MySQLDA.Fill(ReturnDT)
End Using
bReturn = ReturnDT.Rows.Count
If bReturn <= 0 Then
sGenerateError("No Records Returned For Data Table")
'Celar Data Table
ReturnDT.Clear()
End If
'Send Default output Variable
Return bReturn
End Function
Public Function fDataTableSP(ByRef ReturnDT As DataTable, _
ByVal stProcedure As String) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
ReturnDT.Clear()
Using MySQLCMD As New MySqlCommand(stProcedure, mMySqlConnection)
MySQLCMD.CommandType = CommandType.StoredProcedure
'Start Using a New DataAdapter with New MySqlCommand
Using MySQLDA As New MySqlDataAdapter(MySQLCMD)
'Fill DataTable From MySql Adapter
MySQLDA.Fill(ReturnDT)
End Using
End Using
bReturn = ReturnDT.Rows.Count
If bReturn <= 0 Then
sGenerateError("No Records Returned For Data Table")
'Celar Data Table
ReturnDT.Clear()
End If
'Send Default output Variable
Return bReturn
End Function
Public Function fDataView(ByRef ReturnDV As DataView, _
ByVal stSQLStatement As String) As Long
'Returns Number Of Records Affected
Dim mDataTable As New DataTable
Dim bReturn As Long = fDataTable(mDataTable, stSQLStatement)
If bReturn > 0 Then
ReturnDV = New DataView(mDataTable)
Else
'Cleanup
If Not mDataTable Is Nothing Then
mDataTable.Clear()
mDataTable.Dispose()
mDataTable = Nothing
End If
End If
'Send Default output Variable
Return bReturn
End Function
Public Function fDataViewSP(ByRef ReturnDV As DataView, _
ByVal stProcedure As String) As Long
'Returns Number Of Records Affected
Dim mDataTable As New DataTable
Dim bReturn As Long = fDataTable(mDataTable, stProcedure)
If bReturn > 0 Then
ReturnDV = New DataView(mDataTable)
bReturn = ReturnDV.Count
Else
'Cleanup
If Not mDataTable Is Nothing Then
mDataTable.Clear()
mDataTable.Dispose()
mDataTable = Nothing
End If
End If
'Send Default output Variable
Return bReturn
End Function
Public Function fExecute(ByVal stSQLStatement As String) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
Using MySQLCMD As New MySqlCommand(stSQLStatement, mMySqlConnection)
MySQLCMD.CommandType = CommandType.Text
'Execute SQL and Count Records Affected
bReturn = MySQLCMD.ExecuteNonQuery()
End Using
Return bReturn
End Function
Public Function fExecute(ByVal stSQLStatement As String, ByVal bGenerateError As Boolean) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
bReturn = fExecute(stSQLStatement)
If bReturn <= 0 And bGenerateError = True Then
sGenerateError("Failed to Effect Any Records")
End If
Return bReturn
End Function
Public Function fMySqlDataReader(ByRef ReturnRDR As MySqlDataReader, _
ByVal stSQLStatement As String, _
Optional ByVal CBehaviour As System.Data.CommandBehavior = CommandBehavior.Default) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
'Clear and Set Ouput Buffers
If Not ReturnRDR Is Nothing Then
If ReturnRDR.IsClosed = False Then
ReturnRDR.Close()
End If
ReturnRDR.Dispose()
ReturnRDR = Nothing
End If
'Only proceed if Reader not already busy
If mIsReaderBusy = False Then
'Run Count Query to Count Rows
Dim iFindFrom As Integer = InStr(stSQLStatement.ToUpper, "FROM")
Dim stCriteria As String = Mid(stSQLStatement, iFindFrom + 5)
Dim stSQLCount As String = "SELECT Count(*) as RECCOUNT FROM " & stCriteria
'Get Record Count
Using MySQLCMDCount As New MySqlCommand(stSQLCount, mMySqlConnection)
bReturn = MySQLCMDCount.ExecuteScalar
End Using
'Return Reader if Records Exist
If bReturn > 0 Then
Using MySQLCMD As New MySqlCommand(stSQLStatement, mMySqlConnection)
'DebugStr(stSQL)
'Set Data Reader Busy Variable and Show INformation
With MySQLCMD
.CommandType = CommandType.Text
Dim iaSync As IAsyncResult
'Set Reader Busy Flag
mIsReaderBusy = True
iaSync = .BeginExecuteReader(CBehaviour)
'Load Buffer From Object
ReturnRDR = .EndExecuteReader(iaSync)
Do Until iaSync.IsCompleted = True
'Wait Until Finished
Loop
End With
End Using
Else
sGenerateError("No Records Returned For Reader")
'Dont Need to Clear Reader as Should already be empty
End If
Else
sGenerateError("Another Reader is Open")
End If
'Send Default output Variable
Return bReturn
End Function
Public Function fMySqlDataReaderSP(ByRef ReturnRDR As MySqlDataReader, _
ByVal stProcedure As String, _
Optional ByVal CBehaviour As System.Data.CommandBehavior = CommandBehavior.Default) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
'Clear and Set Ouput Buffers
If Not ReturnRDR Is Nothing Then
If ReturnRDR.IsClosed = False Then
ReturnRDR.Close()
End If
ReturnRDR.Dispose()
ReturnRDR = Nothing
End If
'Only proceed if Reader not already busy
If mIsReaderBusy = False Then
'Run Count Query to Count Rows
Dim stCriteria As String = stProcedure
Dim stSQLCount As String = "SELECT Count(*) as RECCOUNT FROM " & stCriteria
'Get Record Count
Using MySQLCMDCount As New MySqlCommand(stSQLCount, mMySqlConnection)
bReturn = MySQLCMDCount.ExecuteScalar
End Using
'Return Reader if Records Exist
If bReturn > 0 Then
Using MySQLCMD As New MySqlCommand(stProcedure, mMySqlConnection)
'DebugStr(stSQL)
'Set Data Reader Busy Variable and Show INformation
With MySQLCMD
.CommandType = CommandType.StoredProcedure
Dim iaSync As IAsyncResult
'Set Reader Busy Flag
mIsReaderBusy = True
iaSync = .BeginExecuteReader(CBehaviour)
'Load Buffer From Object
ReturnRDR = .EndExecuteReader(iaSync)
Do Until iaSync.IsCompleted = True
'Wait Until Finished
Loop
End With
End Using
Else
sGenerateError("No Records Returned For Reader")
'Dont Need to Clear Reader as Should already be empty
End If
Else
sGenerateError("Another Reader is Open")
End If
'Send Default output Variable
Return bReturn
End Function
#End Region
#Region "Advanced Control Data Methods"
Public Sub sSet_DataGridView_Bound(ByRef pDataGridViewControl As DataGridView, _
ByVal stSQLStatement As String)
Dim DataV As DataView = Nothing
If fDataView(DataV, stSQLStatement) > 0 Then
'format data Grid
'Load Data By Binding Data View
pDataGridViewControl.DataSource = DataV
Else
'No Data
End If
End Sub
Public Sub sSet_DataGridView_UnBound(ByRef pDataGridViewControl As DataGridView, _
ByVal stSQLStatement As String)
'Returns Number Of Records Affected
Dim bReturn As Long = 0
Dim RowArray() As Object = Nothing
Dim mySqlRDR As MySqlDataReader = Nothing
bReturn = fMySqlDataReader(mySqlRDR, stSQLStatement, CommandBehavior.SingleResult)
If bReturn > 0 And mySqlRDR.HasRows Then
bReturn = 0
With mySqlRDR
pDataGridViewControl.ColumnCount = .FieldCount
While .Read
'Load Array Line By Line From Data Reader
ReDim RowArray(.FieldCount - 1)
.GetValues(RowArray)
pDataGridViewControl.Rows.Add(RowArray)
bReturn += 1
End While
End With
End If
'Close Data Reader
sMySQLDataReader_Close(mySqlRDR)
End Sub
Public Sub sSet_FlexGridView_Bound(ByRef pFlexGridViewControl As C1.Win.C1FlexGrid.C1FlexGrid, _
ByVal stSQLStatement As String)
Dim DataV As DataView = Nothing
If fDataView(DataV, stSQLStatement) > 0 Then
'format data Grid
'Load Data By Binding Data View
pFlexGridViewControl.DataSource = DataV
Else
'No Data
End If
End Sub
#End Region
Public Function fConvertMySQLDateTimeToDate(ByVal inDate As MySql.Data.Types.MySqlDateTime) As Date
Dim bReturn As Date
Dim inYear As Integer
Dim inMonth As Integer
Dim inDay As Integer
Dim inHour As Integer
Dim inMin As Integer
Dim inSec As Integer
Dim outDate As Date
Dim outTime As Date
inYear = inDate.Year ' Microsoft.VisualBasic.DateAndTime.Year(inDate)
inMonth = inDate.Month 'Microsoft.VisualBasic.DateAndTime.Month(inDate)
inDay = inDate.Day 'Microsoft.VisualBasic.DateAndTime.Day(inDate)
inHour = inDate.Hour 'Microsoft.VisualBasic.DateAndTime.Hour(inDate)
inMin = inDate.Minute 'Microsoft.VisualBasic.DateAndTime.Minute(inDate)
inSec = inDate.Second 'Microsoft.VisualBasic.DateAndTime.Second(inDate)
outDate = DateAndTime.DateSerial(inYear, inMonth, inDay)
outTime = DateAndTime.TimeSerial(inHour, inMin, inSec)
bReturn = CDate(outDate & " " & outTime)
Return bReturn
End Function
Public Function fConvertDateToMySQLDateTime(ByVal inDate As Date) As MySql.Data.Types.MySqlDateTime
Dim bReturn As MySql.Data.Types.MySqlDateTime
Dim inYear As Integer
Dim inMonth As Integer
Dim inDay As Integer
Dim inHour As Integer
Dim inMin As Integer
Dim inSec As Integer
inYear = inDate.Year ' Microsoft.VisualBasic.DateAndTime.Year(inDate)
inMonth = inDate.Month 'Microsoft.VisualBasic.DateAndTime.Month(inDate)
inDay = inDate.Day 'Microsoft.VisualBasic.DateAndTime.Day(inDate)
inHour = inDate.Hour 'Microsoft.VisualBasic.DateAndTime.Hour(inDate)
inMin = inDate.Minute 'Microsoft.VisualBasic.DateAndTime.Minute(inDate)
inSec = inDate.Second 'Microsoft.VisualBasic.DateAndTime.Second(inDate)
bReturn.Year = inYear
bReturn.Month = inMonth
bReturn.Day = inDay
bReturn.Hour = inHour
bReturn.Minute = inMin
bReturn.Second = inSec
Return bReturn
End Function
End Class
Im Pretty New to Developing on Net and have started in VB Net 2010. After Many searches looking for a sample of Connecting and Running Data Queries etc. and coming up short, I have made the following Class in VB Net 2010. I know its not fantastic But it should be able to give some scope on how to do things.
Please any feedback and Improvements would be greatly appreciated!
Code:
Imports MySql.Data.MySqlClient
Public Class clsMySql
#Region "IDisposable Object ..."
Implements IDisposable
Private disposedValue As Boolean = False
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free unmanaged resources when explicitly called
End If
' TODO: free shared unmanaged resources
Call sDisConnect()
End If
Me.disposedValue = True
End Sub
#End Region
#Region "Events Declaration"
Event eErrorOccured(ByVal stMessage As String)
Event eExceptionOccured(ByVal exErr As Exception)
#End Region
#Region "Class Properties"
#Region "Basic Class Properties"
Private mIsReaderBusy As Boolean = False
Public ReadOnly Property pIsReaderBusy() As Boolean
Get
Return mIsReaderBusy
End Get
End Property
Private mLastError As String
Public ReadOnly Property pLastError() As String
Get
Return mLastError
End Get
End Property
Private mLastExeption As Exception
Public ReadOnly Property pLastExeption() As Exception
Get
Return mLastExeption
End Get
End Property
Private WithEvents mMySqlConnection As MySqlConnection
Public ReadOnly Property pMySqlConnection() As MySqlConnection
Get
Return mMySqlConnection
End Get
End Property
Private mMySqlConnectionString As String
Public Property pMySqlConnectionString() As String
Get
Return mMySqlConnectionString
End Get
Set(ByVal value As String)
mMySqlConnectionString = value
End Set
End Property
#End Region
#Region "Advanced Class Properties"
Private mToolStripMenuItem As ToolStripMenuItem
Public Sub pSetToolStripMenuItem(ByRef inToolStripMenuItem As ToolStripMenuItem)
mToolStripMenuItem = inToolStripMenuItem
End Sub
Public Function pGetToolStripMenuItem()
Return mToolStripMenuItem
End Function
Private mToolStripStatusLabel As ToolStripStatusLabel
Public Sub pSetToolStripStatusLabel(ByRef inToolStripStatusLabel As ToolStripStatusLabel)
mToolStripStatusLabel = inToolStripStatusLabel
End Sub
Public Function pGetToolStripStatusLabel()
Return mToolStripStatusLabel
End Function
Public ReadOnly Property pDatabase() As String
Get
Return mMySqlConnection.Database
End Get
End Property
#End Region
#End Region
#Region "Connection Object Event Handlers"
Private Sub mMySqlConnection_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles mMySqlConnection.Disposed
Try
If (Not mToolStripStatusLabel Is Nothing) Then
mToolStripStatusLabel.ToolTipText = mToolStripStatusLabel.Tag & " - " & "Not Available"
mToolStripStatusLabel.Image = My.Resources.Resources.p16MySQL
End If
If (Not mToolStripMenuItem Is Nothing) Then
mToolStripMenuItem.Checked = False
End If
Catch exErr As Exception
sGenerateException(exErr)
End Try
End Sub
Private Sub mMySqlConnection_StateChange(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs) Handles mMySqlConnection.StateChange
Dim stToolTip As String
Dim imgStatus As Image = My.Resources.Resources.p16MySQL
Dim chkMenu As Boolean = False
Try
Select Case e.CurrentState
Case ConnectionState.Open
stToolTip = "Connected"
imgStatus = My.Resources.Resources.p16MySQL2
chkMenu = True
Case ConnectionState.Closed
stToolTip = "Closed"
Case ConnectionState.Broken
stToolTip = "Broken"
Case ConnectionState.Connecting
stToolTip = "Connecting"
imgStatus = My.Resources.Resources.p16MySQL3
chkMenu = True
Case ConnectionState.Executing
stToolTip = "Executing"
imgStatus = My.Resources.Resources.p16MySQL3
chkMenu = True
Case ConnectionState.Fetching
stToolTip = "Fetching"
imgStatus = My.Resources.Resources.p16MySQL3
chkMenu = True
Case Else
stToolTip = "Unknown State"
End Select
If (Not mToolStripStatusLabel Is Nothing) Then
mToolStripStatusLabel.Tag = mMySqlConnection.Database
mToolStripStatusLabel.Text = ""
stToolTip = mToolStripStatusLabel.Tag & " - " & stToolTip
mToolStripStatusLabel.ToolTipText = stToolTip
mToolStripStatusLabel.Image = imgStatus
End If
If (Not mToolStripMenuItem Is Nothing) Then
mToolStripMenuItem.Checked = chkMenu
End If
Catch exErr As Exception
sGenerateException(exErr)
End Try
End Sub
#End Region
#Region "Output Events Methods"
Private Sub sGenerateError(ByVal stMessage As String)
mLastError = stMessage
RaiseEvent eErrorOccured(stMessage)
End Sub
Private Sub sGenerateException(ByVal exErr As Exception, _
Optional ByVal bIncludeErr As Boolean = False, _
Optional ByVal stMessage As String = "")
mLastExeption = exErr
RaiseEvent eExceptionOccured(exErr)
If bIncludeErr = True Then
RaiseEvent eErrorOccured(stMessage)
End If
End Sub
#End Region
#Region "Basic Class Methods"
Public Sub New()
End Sub
Public Sub New(ByVal stConnectionString As String)
mMySqlConnectionString = stConnectionString
End Sub
Public Sub New(ByVal stConnectionString As String, _
ByRef inToolStripMenuItem As ToolStripMenuItem)
mMySqlConnectionString = stConnectionString
mToolStripMenuItem = inToolStripMenuItem
End Sub
Public Sub New(ByVal stConnectionString As String, _
ByRef inToolStripMenuItem As ToolStripMenuItem, _
ByRef inToolStripStatusLabel As ToolStripStatusLabel)
mMySqlConnectionString = stConnectionString
mToolStripMenuItem = inToolStripMenuItem
mToolStripStatusLabel = inToolStripStatusLabel
End Sub
#End Region
#Region "Advanced Class Methods"
Public Sub sConnect()
Dim bReturn As Boolean = fConnect()
If bReturn = False Then
sGenerateError("Failed to Connect to Database")
End If
End Sub
Public Sub sConnect(ByVal stConnectionstring As String)
mMySqlConnectionString = stConnectionstring
fConnect()
End Sub
Public Sub sConnect(ByVal stConnectionString As String, _
ByRef inToolStripMenuItem As ToolStripMenuItem)
mToolStripMenuItem = inToolStripMenuItem
sConnect(stConnectionString)
End Sub
Public Sub sConnect(ByVal stConnectionString As String, _
ByRef inToolStripMenuItem As ToolStripMenuItem, _
ByRef inToolStripStatusLabel As ToolStripStatusLabel)
mToolStripStatusLabel = inToolStripStatusLabel
sConnect(stConnectionString, inToolStripMenuItem)
End Sub
Public Sub sDisConnect()
Dim bReturn As Boolean = fDisConnect()
If bReturn = False Then
sGenerateError("Failed to DisConnect from Database")
End If
End Sub
Public Sub sVerify()
Dim bReturn As Boolean = fVerify()
If bReturn = False Then
sGenerateError("Failed to Verify Database")
End If
End Sub
Public Sub sVerify(ByVal bReconnect As Boolean)
Dim bReturn As Boolean = fVerify(bReconnect)
If bReturn = False Then
sGenerateError("Failed to Verify Database")
End If
End Sub
Public Sub sMySQLDataReader_Close(ByRef MySQLRDR As MySqlDataReader)
Try
'Close and Dispose Datareader object
If Not MySQLRDR Is Nothing Then
If MySQLRDR.IsClosed = False Then
MySQLRDR.Close()
End If
MySQLRDR.Dispose()
MySQLRDR = Nothing
End If
'Reset Datareader Marker
mIsReaderBusy = False
Catch exErr As Exception
sGenerateException(exErr)
End Try
End Sub
#End Region
#Region "Advanced Class Functions"
Public Function fConnect() As Boolean
Dim bReturn As Boolean = False
Try
'Close Exisiting Connection
If (Not mMySqlConnection Is Nothing) Then
'Close any Exisiting Connections
If mMySqlConnection.State = ConnectionState.Open Then
Call sDisConnect()
End If
End If
'Create New Connection
mMySqlConnection = New MySqlConnection
With mMySqlConnection
.ConnectionString = mMySqlConnectionString
.Open()
End With
bReturn = True
Catch exErr As Exception
sGenerateException(exErr)
End Try
Return bReturn
End Function
Public Function fConnect(ByVal stConnectionstring As String) As Boolean
mMySqlConnectionString = stConnectionstring
Return fConnect()
End Function
Public Function fConnect(ByVal stConnectionString As String, _
ByRef inToolStripMenuItem As ToolStripMenuItem) As Boolean
mToolStripMenuItem = inToolStripMenuItem
Return fConnect(stConnectionString)
End Function
Public Function fConnect(ByVal stConnectionString As String, _
ByRef inToolStripMenuItem As ToolStripMenuItem, _
ByRef inToolStripStatusLabel As ToolStripStatusLabel) As Boolean
mToolStripStatusLabel = inToolStripStatusLabel
Return fConnect(stConnectionString, inToolStripMenuItem)
End Function
Public Function fDisConnect() As Boolean
Dim bReturn As Boolean = False
Try
'Clear Object Variables
If (Not mMySqlConnection Is Nothing) Then
If mMySqlConnection.State <> ConnectionState.Closed Then
mMySqlConnection.Close()
End If
mMySqlConnection.Dispose()
mMySqlConnection = Nothing
End If
bReturn = True
Catch exErr As Exception
sGenerateException(exErr)
End Try
Return bReturn
End Function
Public Function fVerify() As Boolean
Dim bReturn As Boolean = False
Try
If (Not mMySqlConnection Is Nothing) Then
If (mMySqlConnection.State = ConnectionState.Open) Then
Using MySQLCMD As New MySqlCommand("Show Tables", mMySqlConnection)
'Set Data Reader Busy Variable
MySQLCMD.CommandType = CommandType.Text
Dim MySQLRDR As MySqlDataReader
Dim iaSync As IAsyncResult
'If Data Reader is Busy Then Connection is Verified
If mIsReaderBusy = False Then
mIsReaderBusy = True
iaSync = MySQLCMD.BeginExecuteReader(CommandBehavior.SingleRow)
MySQLRDR = MySQLCMD.EndExecuteReader(iaSync)
Do Until iaSync.IsCompleted = True
'Wait until aSync Reader Finished
Loop
'Close Reader as we have finished with it
sMySQLDataReader_Close(MySQLRDR)
End If
End Using
bReturn = True
End If
End If
Catch exErr As Exception
sGenerateException(exErr)
Finally
'Disconnect if Failed Verify
If bReturn = False Then
sDisConnect()
End If
End Try
Return bReturn
End Function
Public Function fVerify(ByVal bReconnect As Boolean) As Boolean
Dim bReturn As Boolean = fVerify()
If bReturn = False And bReconnect = True Then
bReturn = fConnect()
End If
Return bReturn
End Function
#End Region
#Region "Advanced Data Methods"
#End Region
#Region "Advanced Data Functions"
Public Function fRowArrayFromReader(ByRef ReturnArray() As Object, _
ByVal stSQLStatement As String) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
Dim OutArray() As Object = Nothing
Dim mySqlRDR As MySqlDataReader = Nothing
ReturnArray = Nothing
bReturn = fMySqlDataReader(mySqlRDR, stSQLStatement, CommandBehavior.SingleResult)
If bReturn > 0 And mySqlRDR.HasRows Then
bReturn = 0
With mySqlRDR
While .Read
'Load Array Line By Line From Data Reader
ReDim OutArray(.FieldCount - 1)
.GetValues(OutArray)
bReturn += 1
End While
End With
End If
'Close Data Reader
sMySQLDataReader_Close(mySqlRDR)
'Send Default output Variable anbd Buffer
ReturnArray = OutArray
Return bReturn
End Function
Public Function fDataTable(ByRef ReturnDT As DataTable, _
ByVal stSQLStatement As String) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
ReturnDT.Clear()
'Start Using a New DataAdapter with New MySqlCommand
Using MySQLDA As New MySqlDataAdapter()
MySQLDA.SelectCommand = New MySqlCommand(stSQLStatement, mMySqlConnection)
'Fill DataTable From MySql Adapter
MySQLDA.Fill(ReturnDT)
End Using
bReturn = ReturnDT.Rows.Count
If bReturn <= 0 Then
sGenerateError("No Records Returned For Data Table")
'Celar Data Table
ReturnDT.Clear()
End If
'Send Default output Variable
Return bReturn
End Function
Public Function fDataTableSP(ByRef ReturnDT As DataTable, _
ByVal stProcedure As String) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
ReturnDT.Clear()
Using MySQLCMD As New MySqlCommand(stProcedure, mMySqlConnection)
MySQLCMD.CommandType = CommandType.StoredProcedure
'Start Using a New DataAdapter with New MySqlCommand
Using MySQLDA As New MySqlDataAdapter(MySQLCMD)
'Fill DataTable From MySql Adapter
MySQLDA.Fill(ReturnDT)
End Using
End Using
bReturn = ReturnDT.Rows.Count
If bReturn <= 0 Then
sGenerateError("No Records Returned For Data Table")
'Celar Data Table
ReturnDT.Clear()
End If
'Send Default output Variable
Return bReturn
End Function
Public Function fDataView(ByRef ReturnDV As DataView, _
ByVal stSQLStatement As String) As Long
'Returns Number Of Records Affected
Dim mDataTable As New DataTable
Dim bReturn As Long = fDataTable(mDataTable, stSQLStatement)
If bReturn > 0 Then
ReturnDV = New DataView(mDataTable)
Else
'Cleanup
If Not mDataTable Is Nothing Then
mDataTable.Clear()
mDataTable.Dispose()
mDataTable = Nothing
End If
End If
'Send Default output Variable
Return bReturn
End Function
Public Function fDataViewSP(ByRef ReturnDV As DataView, _
ByVal stProcedure As String) As Long
'Returns Number Of Records Affected
Dim mDataTable As New DataTable
Dim bReturn As Long = fDataTable(mDataTable, stProcedure)
If bReturn > 0 Then
ReturnDV = New DataView(mDataTable)
bReturn = ReturnDV.Count
Else
'Cleanup
If Not mDataTable Is Nothing Then
mDataTable.Clear()
mDataTable.Dispose()
mDataTable = Nothing
End If
End If
'Send Default output Variable
Return bReturn
End Function
Public Function fExecute(ByVal stSQLStatement As String) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
Using MySQLCMD As New MySqlCommand(stSQLStatement, mMySqlConnection)
MySQLCMD.CommandType = CommandType.Text
'Execute SQL and Count Records Affected
bReturn = MySQLCMD.ExecuteNonQuery()
End Using
Return bReturn
End Function
Public Function fExecute(ByVal stSQLStatement As String, ByVal bGenerateError As Boolean) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
bReturn = fExecute(stSQLStatement)
If bReturn <= 0 And bGenerateError = True Then
sGenerateError("Failed to Effect Any Records")
End If
Return bReturn
End Function
Public Function fMySqlDataReader(ByRef ReturnRDR As MySqlDataReader, _
ByVal stSQLStatement As String, _
Optional ByVal CBehaviour As System.Data.CommandBehavior = CommandBehavior.Default) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
'Clear and Set Ouput Buffers
If Not ReturnRDR Is Nothing Then
If ReturnRDR.IsClosed = False Then
ReturnRDR.Close()
End If
ReturnRDR.Dispose()
ReturnRDR = Nothing
End If
'Only proceed if Reader not already busy
If mIsReaderBusy = False Then
'Run Count Query to Count Rows
Dim iFindFrom As Integer = InStr(stSQLStatement.ToUpper, "FROM")
Dim stCriteria As String = Mid(stSQLStatement, iFindFrom + 5)
Dim stSQLCount As String = "SELECT Count(*) as RECCOUNT FROM " & stCriteria
'Get Record Count
Using MySQLCMDCount As New MySqlCommand(stSQLCount, mMySqlConnection)
bReturn = MySQLCMDCount.ExecuteScalar
End Using
'Return Reader if Records Exist
If bReturn > 0 Then
Using MySQLCMD As New MySqlCommand(stSQLStatement, mMySqlConnection)
'DebugStr(stSQL)
'Set Data Reader Busy Variable and Show INformation
With MySQLCMD
.CommandType = CommandType.Text
Dim iaSync As IAsyncResult
'Set Reader Busy Flag
mIsReaderBusy = True
iaSync = .BeginExecuteReader(CBehaviour)
'Load Buffer From Object
ReturnRDR = .EndExecuteReader(iaSync)
Do Until iaSync.IsCompleted = True
'Wait Until Finished
Loop
End With
End Using
Else
sGenerateError("No Records Returned For Reader")
'Dont Need to Clear Reader as Should already be empty
End If
Else
sGenerateError("Another Reader is Open")
End If
'Send Default output Variable
Return bReturn
End Function
Public Function fMySqlDataReaderSP(ByRef ReturnRDR As MySqlDataReader, _
ByVal stProcedure As String, _
Optional ByVal CBehaviour As System.Data.CommandBehavior = CommandBehavior.Default) As Long
'Returns Number Of Records Affected
Dim bReturn As Long = 0
'Clear and Set Ouput Buffers
If Not ReturnRDR Is Nothing Then
If ReturnRDR.IsClosed = False Then
ReturnRDR.Close()
End If
ReturnRDR.Dispose()
ReturnRDR = Nothing
End If
'Only proceed if Reader not already busy
If mIsReaderBusy = False Then
'Run Count Query to Count Rows
Dim stCriteria As String = stProcedure
Dim stSQLCount As String = "SELECT Count(*) as RECCOUNT FROM " & stCriteria
'Get Record Count
Using MySQLCMDCount As New MySqlCommand(stSQLCount, mMySqlConnection)
bReturn = MySQLCMDCount.ExecuteScalar
End Using
'Return Reader if Records Exist
If bReturn > 0 Then
Using MySQLCMD As New MySqlCommand(stProcedure, mMySqlConnection)
'DebugStr(stSQL)
'Set Data Reader Busy Variable and Show INformation
With MySQLCMD
.CommandType = CommandType.StoredProcedure
Dim iaSync As IAsyncResult
'Set Reader Busy Flag
mIsReaderBusy = True
iaSync = .BeginExecuteReader(CBehaviour)
'Load Buffer From Object
ReturnRDR = .EndExecuteReader(iaSync)
Do Until iaSync.IsCompleted = True
'Wait Until Finished
Loop
End With
End Using
Else
sGenerateError("No Records Returned For Reader")
'Dont Need to Clear Reader as Should already be empty
End If
Else
sGenerateError("Another Reader is Open")
End If
'Send Default output Variable
Return bReturn
End Function
#End Region
#Region "Advanced Control Data Methods"
Public Sub sSet_DataGridView_Bound(ByRef pDataGridViewControl As DataGridView, _
ByVal stSQLStatement As String)
Dim DataV As DataView = Nothing
If fDataView(DataV, stSQLStatement) > 0 Then
'format data Grid
'Load Data By Binding Data View
pDataGridViewControl.DataSource = DataV
Else
'No Data
End If
End Sub
Public Sub sSet_DataGridView_UnBound(ByRef pDataGridViewControl As DataGridView, _
ByVal stSQLStatement As String)
'Returns Number Of Records Affected
Dim bReturn As Long = 0
Dim RowArray() As Object = Nothing
Dim mySqlRDR As MySqlDataReader = Nothing
bReturn = fMySqlDataReader(mySqlRDR, stSQLStatement, CommandBehavior.SingleResult)
If bReturn > 0 And mySqlRDR.HasRows Then
bReturn = 0
With mySqlRDR
pDataGridViewControl.ColumnCount = .FieldCount
While .Read
'Load Array Line By Line From Data Reader
ReDim RowArray(.FieldCount - 1)
.GetValues(RowArray)
pDataGridViewControl.Rows.Add(RowArray)
bReturn += 1
End While
End With
End If
'Close Data Reader
sMySQLDataReader_Close(mySqlRDR)
End Sub
Public Sub sSet_FlexGridView_Bound(ByRef pFlexGridViewControl As C1.Win.C1FlexGrid.C1FlexGrid, _
ByVal stSQLStatement As String)
Dim DataV As DataView = Nothing
If fDataView(DataV, stSQLStatement) > 0 Then
'format data Grid
'Load Data By Binding Data View
pFlexGridViewControl.DataSource = DataV
Else
'No Data
End If
End Sub
#End Region
Public Function fConvertMySQLDateTimeToDate(ByVal inDate As MySql.Data.Types.MySqlDateTime) As Date
Dim bReturn As Date
Dim inYear As Integer
Dim inMonth As Integer
Dim inDay As Integer
Dim inHour As Integer
Dim inMin As Integer
Dim inSec As Integer
Dim outDate As Date
Dim outTime As Date
inYear = inDate.Year ' Microsoft.VisualBasic.DateAndTime.Year(inDate)
inMonth = inDate.Month 'Microsoft.VisualBasic.DateAndTime.Month(inDate)
inDay = inDate.Day 'Microsoft.VisualBasic.DateAndTime.Day(inDate)
inHour = inDate.Hour 'Microsoft.VisualBasic.DateAndTime.Hour(inDate)
inMin = inDate.Minute 'Microsoft.VisualBasic.DateAndTime.Minute(inDate)
inSec = inDate.Second 'Microsoft.VisualBasic.DateAndTime.Second(inDate)
outDate = DateAndTime.DateSerial(inYear, inMonth, inDay)
outTime = DateAndTime.TimeSerial(inHour, inMin, inSec)
bReturn = CDate(outDate & " " & outTime)
Return bReturn
End Function
Public Function fConvertDateToMySQLDateTime(ByVal inDate As Date) As MySql.Data.Types.MySqlDateTime
Dim bReturn As MySql.Data.Types.MySqlDateTime
Dim inYear As Integer
Dim inMonth As Integer
Dim inDay As Integer
Dim inHour As Integer
Dim inMin As Integer
Dim inSec As Integer
inYear = inDate.Year ' Microsoft.VisualBasic.DateAndTime.Year(inDate)
inMonth = inDate.Month 'Microsoft.VisualBasic.DateAndTime.Month(inDate)
inDay = inDate.Day 'Microsoft.VisualBasic.DateAndTime.Day(inDate)
inHour = inDate.Hour 'Microsoft.VisualBasic.DateAndTime.Hour(inDate)
inMin = inDate.Minute 'Microsoft.VisualBasic.DateAndTime.Minute(inDate)
inSec = inDate.Second 'Microsoft.VisualBasic.DateAndTime.Second(inDate)
bReturn.Year = inYear
bReturn.Month = inMonth
bReturn.Day = inDay
bReturn.Hour = inHour
bReturn.Minute = inMin
bReturn.Second = inSec
Return bReturn
End Function
End Class