Imports System.Data.SqlClient
Module modMisc
#Region "Declarations ..."
Private blnHaveCleared(1) As Boolean
Private colMasterHeaders() As clsColumnReference
Private colSecondHeaders() As clsColumnReference
Private colThirdHeaders() As clsColumnReference
Private Const constMasterHeader As Long = 0
Private Const constSecondaryHeader As Long = 1
Private Const constThirdHeader As Long = 2
#End Region
#Region "clsColumnReference()"
Class clsColumnReference
Public strColumnText As String
Public lngParentIndex As Long
Public blnIsMasterParent As Boolean
Public strChildSAPCodes As String
End Class
#End Region
#Region "sqlAddProductToDB()"
Public Sub sqlAddProductToDB(ByVal strData As String, ByVal mySqlConnection As SqlConnection)
Dim strSQLStatement As String
If Not blnHaveCleared(0) Then
strSQLStatement = "DELETE FROM tblProducts WHERE strProductReference = strProductReference;"
Else
strData = Replace$(strData, "'", vbNullString)
strData = Replace$(strData, vbTab, "','")
strSQLStatement = _
"INSERT INTO tblProducts " & _
"(strProductReference, strSectionText, strShortDescription, strDetailURL, strTradePrice, strPrice, strDetailLink, strFullDescription, strRRP, strStock) VALUES " & _
"(" & "'" & strData & "'" & ");"
End If
Dim mySQLCommand As New SqlCommand(strSQLStatement, mySqlConnection)
'ListBox1.Items.Add(strPrepare(strSQLStatement))
Try
mySQLCommand.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message & vbCrLf & ex.StackTrace & vbCrLf & vbCrLf & strSQLStatement)
End Try
If Not blnHaveCleared(0) Then
blnHaveCleared(0) = True
sqlAddProductToDB(strData, mySqlConnection)
End If
End Sub
#End Region
#Region "doInitHeaders()"
Private Sub doInitHeaders()
If colMasterHeaders Is Nothing Then ReDim colMasterHeaders(0) : colMasterHeaders(0) = New clsColumnReference()
If colSecondHeaders Is Nothing Then ReDim colSecondHeaders(0) : colSecondHeaders(0) = New clsColumnReference()
If colThirdHeaders Is Nothing Then ReDim colThirdHeaders(0) : colThirdHeaders(0) = New clsColumnReference()
End Sub
#End Region
#Region "sqlAddHeaders()"
Public Sub sqlAddHeaders(ByVal strData As String, ByVal strChildSAPCode As String, ByVal mySqlConnection As SqlConnection)
Dim i As Long, strHeaders() As String = Split(strData, "|"), lngParentIndex As Long
Dim lngHeaderIndex(2) As Long
Dim blnFoundText As Boolean = False : doInitHeaders()
Dim blnAddHeader(2) As Boolean
Dim strHeaderTable(2) As String
strHeaderTable(0) = "tblMasterHeaders"
strHeaderTable(1) = "tblSecondaryHeaders"
strHeaderTable(2) = "tblThirdHeaders"
For i = 0 To 2
blnAddHeader(i) = False
Next
'''''''''''''''''''''''''''''
'' Split up column headers ''
'''''''''''''''''''''''''''''
'' Master Headers
''
For i = 0 To UBound(colMasterHeaders)
With colMasterHeaders(i)
If strHeaders(0) = .strColumnText Then
blnFoundText = True
lngParentIndex = i
Exit For
End If
End With
Next
If Not blnFoundText Then
ReDim Preserve colMasterHeaders(UBound(colMasterHeaders) + 1)
colMasterHeaders(UBound(colMasterHeaders)) = New clsColumnReference()
With colMasterHeaders(UBound(colMasterHeaders))
.blnIsMasterParent = True
.lngParentIndex = -1
.strColumnText = strHeaders(0)
lngParentIndex = UBound(colMasterHeaders)
blnAddHeader(constMasterHeader) = True
End With
End If
blnFoundText = False
lngHeaderIndex(0) = lngParentIndex
'' Secondary Headers
''
For i = 0 To UBound(colSecondHeaders)
With colSecondHeaders(i)
If strHeaders(1) = .strColumnText Then
blnFoundText = True
lngParentIndex = i
Exit For
End If
End With
Next
If Not blnFoundText Then
ReDim Preserve colSecondHeaders(UBound(colSecondHeaders) + 1)
colSecondHeaders(UBound(colSecondHeaders)) = New clsColumnReference()
With colSecondHeaders(UBound(colSecondHeaders))
.blnIsMasterParent = False
.lngParentIndex = lngParentIndex
.strColumnText = strHeaders(1)
lngParentIndex = i
blnAddHeader(constSecondaryHeader) = True
End With
End If
blnFoundText = False
lngHeaderIndex(1) = lngParentIndex
'' Third Headers
''
For i = 0 To UBound(colThirdHeaders)
If strHeaders(2) = colThirdHeaders(i).strColumnText Then
colThirdHeaders(i).strChildSAPCodes += "," & strChildSAPCode
blnFoundText = True
Exit For
End If
Next
If Not blnFoundText Then
ReDim Preserve colThirdHeaders(UBound(colThirdHeaders) + 1)
colThirdHeaders(UBound(colThirdHeaders)) = New clsColumnReference()
With colThirdHeaders(UBound(colThirdHeaders))
.blnIsMasterParent = False
.lngParentIndex = lngParentIndex
.strColumnText = strHeaders(2)
.strChildSAPCodes = strChildSAPCode
blnAddHeader(constThirdHeader) = True
End With
End If
lngHeaderIndex(2) = lngParentIndex
'''''''''''''''''''''''''''''
'' Insert into SQL DB ''
'''''''''''''''''''''''''''''
Dim strSQLStatement As String, mySQLCommand As SqlCommand
If Not blnHaveCleared(1) Then
For i = 0 To 2
strSQLStatement = "DELETE FROM " & strHeaderTable(i) & " WHERE lngParentIndex = lngParentIndex"
mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
mySQLCommand.ExecuteNonQuery()
Next
mySQLCommand = Nothing
blnHaveCleared(1) = True
End If
If blnAddHeader(0) Then
strSQLStatement = "INSERT INTO " & strHeaderTable(0) & vbCrLf & _
"VALUES ( "
strSQLStatement += lngHeaderIndex(0) & "," & -1 & "," & "'" & colMasterHeaders(lngHeaderIndex(0)).strColumnText & "'"
strSQLStatement += ");"
mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
mySQLCommand.ExecuteNonQuery()
mySQLCommand = Nothing
End If
If blnAddHeader(1) Then
strSQLStatement = "INSERT INTO " & strHeaderTable(1) & vbCrLf & _
"VALUES ( "
strSQLStatement += lngHeaderIndex(1) & "," & colSecondHeaders(lngHeaderIndex(1)).lngParentIndex & ",'" & colSecondHeaders(lngHeaderIndex(1)).strColumnText & "'"
strSQLStatement += ");"
mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
mySQLCommand.ExecuteNonQuery()
mySQLCommand = Nothing
End If
If blnAddHeader(2) Then
strSQLStatement = "SELECT * FROM " & strHeaderTable(2) & " WHERE strColumnText = '" & colThirdHeaders(lngHeaderIndex(2)).strColumnText & "';"
mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
Dim mySQLReader As SqlDataReader
mySQLReader = mySQLCommand.ExecuteReader()
If Not mySQLReader.Read Then
mySQLReader.Close()
strSQLStatement = "INSERT INTO " & strHeaderTable(2) & vbCrLf & _
"VALUES ( "
strSQLStatement += colThirdHeaders(lngHeaderIndex(2)).lngParentIndex & ",'" & colThirdHeaders(lngHeaderIndex(2)).strColumnText & "','" & colThirdHeaders(lngHeaderIndex(2)).strChildSAPCodes & "'"
strSQLStatement += ");"
mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
mySQLCommand.ExecuteNonQuery()
Else
mySQLReader.Close()
strSQLStatement = "UPDATE " & strHeaderTable(2) & vbCrLf & _
"SET strChildSAPCodes = '" & colThirdHeaders(lngHeaderIndex(2)).strChildSAPCodes & "' "
strSQLStatement += " WHERE strColumnText = '" & colThirdHeaders(lngHeaderIndex(2)).strColumnText & "';"
mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
mySQLCommand.ExecuteNonQuery()
End If
mySQLCommand = Nothing
End If
End Sub
#End Region
End Module