Senin, 02 September 2013

Menyederhanakan Eksekusi Store Procedure MySQL dari VB 6

Kali ini saya akan membahas cara Membuat dll sederhana untuk eksekusi Store Procedure MySQL lewat VB 6.
Langkah-langkah nya :
1. Buat project baru dengan menambahkan class module
2. Buat sebuah function dengan nama ExecSP
isi dari bagian Function tersebut adalah :

Public Function ExecSP(strQuery As String) As Boolean
    Dim strDataType As String
    Dim inTypeLength As Integer
    Dim strParamDirection As String
    Dim strParam As String
    Dim adoParam(200) As ADODB.Parameter
    Dim strSplitQuery() As String
    Dim strSplitParam() As String
    Dim i As Integer
    Dim strError As String
   
1
    ExecSP = False
   
    If strQuery = "" Then GoTo error
   
    strSplitQuery = Split(strQuery, ";")
   
    On Error GoTo error
   
    'If ObtainConnection = False Then GoTo error
   
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = Conn
    cmd.CommandType = adCmdStoredProc
    cmd.CommandText = strSplitQuery(0)
   
 
   
    If UBound(strSplitQuery) > 0 Then
   
        For i = 1 To UBound(strSplitQuery)
   
            strSplitParam = Split(strSplitQuery(i), "|")
            strDataType = IIf(strSplitParam(0) = "", "", Left(strSplitParam(0), 1))
            If Len(strSplitParam(0)) > 1 Then
                inTypeLength = CInt(Mid(strSplitParam(0), 2, Len(strSplitParam(0))))
            Else
                inTypeLength = vbNull
            End If
            strParamDirection = IIf(strSplitParam(1) = "", "", strSplitParam(1))
            If UBound(strSplitParam) = 2 Then
                strParam = IIf(strSplitParam(2) = "", "", strSplitParam(2))
            Else
                strParam = ""
            End If
   
            Set adoParam(i) = cmd.CreateParameter(i)
            With adoParam(i)
               .Type = fnParamAttributes(strDataType)
               .Direction = fnParamDirection(strParamDirection)
               If inTypeLength > 0 Then .Size = inTypeLength
               If .Type = 201 Then .Size = 1000000
               If .Type = 7 Then
                  .value = IIf(strParam = "", Null, ConvertDate(strParam))
'                    .value = ConvertDate(strParam)
                Else
                  .value = IIf(strParam = "", Null, strParam)
               End If
            End With
            cmd.Parameters.Append adoParam(i)
   
        Next
   
        Set adoParam(i + 1) = cmd.CreateParameter(i + 1)

    End If

    Set rstRecordset = cmd.Execute
   
    Set cmd.ActiveConnection = Nothing
    ExecSP = True
   
    Exit Function
   
error:
    If Err.Number = -2147467259 Then
        TerminateConnection
        If ConnectMySQL Then
            GoTo 1
        Else
            MsgBox Err.Number & "-" & Err.Description & " " & "Query gagal !"
        End If
    End If

End Function.

untuk script lengkapnya, bisa download disini
kalo masih bingung Cara pakainya silahkan email ke roby.munandar@gmail.com

tx to lost friend Agung