Click here to Skip to main content
15,896,415 members
Articles / Programming Languages / Visual Basic

Simulating Stored Procedures in Microsoft Access using Enterprise Library Application Blocks

Rate me:
Please Sign up or sign in to vote.
3.46/5 (12 votes)
25 Jul 2005MIT6 min read 98.4K   1.1K   37  
Simulating stored procedures in Microsoft Access using Enterprise Library Application Blocks.
'
' ADefwebserver -  http://www.ADefWebserver.com
' Copyright (c) 2005-2006
' by Michael Washington ( webmaster@adefwebserver.com ) of ADefWebserver ( http://www.ADefWebserver.com )
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Reflection
Imports Microsoft.Practices.EnterpriseLibrary.Data
Namespace DALII.Providers.AccessProvider
    Public Class Invoke_Method
        Dim AssemblyDLL As String
        Dim AssemblyCLASS As String

        Dim db As Database = DatabaseFactory.CreateDatabase("CurrentInstance")
        Dim ParameterArray As New ArrayList
        Dim arguments As Object

        Public Function GetData(ByRef command As DBCommandWrapper, ByVal WrapperType As String) As IDataReader
            Dim reader As IDataReader
            Dim myCommandWrapper As DBCommandWrapper = command
            Dim FunctionName As String = myCommandWrapper.Command.CommandText.ToString
            Dim myParameters As New ArrayList
            Dim myOleDBParameter As OleDbParameter
            Dim myOleDBParameterValue As String

            Dim AssemblyName As String
            Dim myassembly As System.Reflection.Assembly
            Dim myassemblyloaded As [Assembly]
            Dim myAssemblyInstance As Object

            ' Get the Assembly name (AssemblyDLL) and class (AssemblyCLASS) for the function
            GetClassandDLL(FunctionName)

            If Not AssemblyDLL = "Not found" Then
                ' Load the Assembly
                AssemblyName = System.IO.Directory.GetCurrentDirectory.ToString() & "\" & AssemblyDLL

                myassemblyloaded = myassembly.LoadFrom(AssemblyName)
                myAssemblyInstance = myassemblyloaded.CreateInstance(AssemblyCLASS)

                Dim myOleDbParameterCollection As OleDbParameterCollection = myCommandWrapper.Command.Parameters

                Dim parameters As Integer = myCommandWrapper.Command.Parameters.Count
                Dim i As Integer
                For i = 1 To parameters
                    myOleDBParameter = myOleDbParameterCollection.Item(i - 1)
                    myOleDBParameterValue = myOleDBParameter.Value
                    myParameters.Add(myOleDBParameterValue)
                Next

                arguments = New Object() {myParameters}
                reader = myAssemblyInstance.GetType.InvokeMember(FunctionName, BindingFlags.InvokeMethod, Nothing, myAssemblyInstance, arguments)

                If WrapperType = "ExecuteNonQuery" Then
                    AppendObjCommandWrapper(command, reader)
                End If

                Return reader
            Else
                Throw New ArgumentException("Function Not found")
            End If

        End Function

        Private Sub AppendObjCommandWrapper(ByRef command As DBCommandWrapper, ByVal myReader As IDataReader)
            Dim myParameter As OleDbParameter
            Dim myOleDbCommandWrapper As DBCommandWrapper
            Dim myParm As Object
            Dim myParameterName As String
            Dim myValue As String
            Dim i As Integer

            Dim myCount As Integer = myReader.FieldCount

            While (myReader.Read())

                If i = myCount Then
                    Exit While
                End If

                myParameterName = myReader.GetName(i).ToString()
                myValue = myReader.GetValue(i).ToString()

                myParameter = New OleDbParameter(myParameterName, OleDbType.VarChar)
                myParameter.Value = myValue
                myParm = myParameter
                command.AddInParameter(myParameterName, DbType.String, myValue)
                i = i + 1
            End While

        End Sub
        Sub GetClassandDLL(ByVal Functioname As String)
            Dim myXMLFile As String = System.IO.Directory.GetCurrentDirectory.ToString()
            myXMLFile = myXMLFile.Replace("\bin", "\")
            myXMLFile += "AccessProvider.xml"
            Dim myserachstrig As String = "FunctionName = '" + Functioname + "'"
            Dim ds As DataSet = New DataSet
            Dim dv As DataView
            Dim id As Integer

            Try
                ' Try to open the XML File
                ds.ReadXml(myXMLFile)
                dv = ds.Tables(0).DefaultView
            Catch
                ' If it can't be found then create it
                CreateXMLFile()
                ds.ReadXml(myXMLFile)
                dv = ds.Tables(0).DefaultView
            End Try

            ' Search for the function
            dv.RowFilter = myserachstrig

            If dv.Count > 0 Then
                AssemblyDLL = dv.Item(0).Item("DLLName").ToString
                AssemblyCLASS = dv.Item(0).Item("ClassName").ToString
            Else
                ' It wasn't found so try creating the XML file again
                CreateXMLFile()

                ' Open it and read it
                ds.Dispose()
                ds.ReadXml(myXMLFile)
                dv = ds.Tables(0).DefaultView

                ' Search for the function
                dv.RowFilter = myserachstrig

                If dv.Count > 0 Then
                    AssemblyDLL = dv.Item(0).Item("DLLName").ToString
                    AssemblyCLASS = dv.Item(0).Item("ClassName").ToString
                Else
                    AssemblyDLL = "Not found"
                    AssemblyCLASS = "Not found"
                End If
            End If
        End Sub

        Sub CreateXMLFile()
            ' make a reference to the "bin\" directory and get the Assemblies
            Dim CurrentDirectory As String = System.IO.Directory.GetCurrentDirectory.ToString()
            CurrentDirectory = CurrentDirectory.Replace("\bin", "\")
            Dim AssemblyDirectory As String = System.IO.Directory.GetCurrentDirectory.ToString() & "\"
            Dim DirectoryInfo As New IO.DirectoryInfo(AssemblyDirectory)
            Dim myFilesInfo As IO.FileInfo() = DirectoryInfo.GetFiles()
            Dim myFileInfo As IO.FileInfo
            Dim myFileName As String
            Dim i As Integer

            Dim myAssemblyFullName As String
            Dim myAssemblyName As String
            Dim myassembly As System.Reflection.Assembly
            Dim myassemblyloaded As [Assembly]

            Dim myAssemblies As New ArrayList

            ' These are all the varaibles needed for the DataTable and DataSet
            Dim myDataTable As New DataTable("Assemblies")
            Dim myDataSet As New DataSet
            Dim myDataRow As DataRow
            Dim FunctionName As DataColumn = New DataColumn("FunctionName")
            FunctionName.DataType = System.Type.GetType("System.String")
            Dim ClassName As DataColumn = New DataColumn("ClassName")
            ClassName.DataType = System.Type.GetType("System.String")
            Dim DLLName As DataColumn = New DataColumn("DLLName")
            DLLName.DataType = System.Type.GetType("System.String")
            myDataTable.Columns.Add(FunctionName)
            myDataTable.Columns.Add(ClassName)
            myDataTable.Columns.Add(DLLName)


            'list the names of all files in the specified directory
            For Each myFileInfo In myFilesInfo
                myFileName = myFileInfo.Name.ToString
                If myFileName.StartsWith("DALII.Providers.AccessProvider_") And myFileName.EndsWith(".dll") Then
                    myAssemblies.Add(myFileName)
                End If
            Next

            Dim myTypes As Type()
            Dim myType As Type
            Dim ModuleNames As System.Reflection.Module()
            Dim ModuleName As System.Reflection.Module
            Dim MemberInfos As System.Reflection.MemberInfo()
            Dim MemberInfo As System.Reflection.MemberInfo

            ' Loop through the Assemblies
            For i = 0 To CInt(myAssemblies.Count) - 1
                myAssemblyFullName = AssemblyDirectory & myAssemblies.Item(i).ToString
                myAssemblyName = myAssemblies.Item(i).ToString
                myassemblyloaded = myassembly.LoadFrom(myAssemblyFullName)
                myTypes = myassemblyloaded.GetTypes()

                ' Retrieve the public members
                For Each myType In myTypes
                    MemberInfos = myType.GetMembers
                    For Each MemberInfo In MemberInfos
                        ' Only add if the member type is a method
                        If MemberInfo.MemberType = MemberTypes.Method Then
                            ' Start a new row
                            myDataRow = myDataTable.NewRow()
                            ' Add items to each column
                            myDataRow.Item("FunctionName") = MemberInfo.Name
                            myDataRow.Item("ClassName") = MemberInfo.ReflectedType.ToString
                            myDataRow.Item("DLLName") = myAssemblyName
                            ' Add the row to the table
                            myDataTable.Rows.Add(myDataRow)
                        End If
                    Next
                Next
            Next i

            ' Add the DataTable to a DataSet
            myDataSet.Tables.Add(myDataTable)

            ' Write the data to a XML File
            myDataSet.WriteXml(CurrentDirectory & "AccessProvider.xml")
        End Sub
    End Class
End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior) http://ADefWebserver.com
United States United States
Michael Washington is a Microsoft MVP. He is a ASP.NET and
C# programmer.
He is the founder of
AiHelpWebsite.com,
LightSwitchHelpWebsite.com, and
HoloLensHelpWebsite.com.

He has a son, Zachary and resides in Los Angeles with his wife Valerie.

He is the Author of:

Comments and Discussions