Adding Interop.ADOX.dll to exe as resource

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I have a project that uses ADOX to make databases from memory using Interop.ADOX.dll

is there a way that i could add the dll file to the exe as a resource and not have the exe file and the dll file to give to people?

meaning can i use the Interop.ADOX.dll from inside the exe file needing it?
 
That can be done with Assembly.Load method, but you have to create the objects using reflection from the loaded assembly, and late binding (Objects). Here is an example of use where ADOX and ADODB is used to create a new Excel book:
VB.NET:
Sub newexcel()
    Dim ax, adb As System.Reflection.Assembly
    ax = Reflection.Assembly.Load(getResource("WindowsApplication1.Interop.ADOX.dll"))
    adb = Reflection.Assembly.Load(getResource("WindowsApplication1.Interop.ADODB.dll"))
 
    Dim cnn As Object = adb.CreateInstance("ADODB.ConnectionClass") 'ADODB.Connection
    Dim cat As Object = ax.CreateInstance("ADOX.CatalogClass") 'ADOX.Catalog
    Dim tbl As Object = ax.CreateInstance("ADOX.TableClass") 'ADOX.Table
 
    cnn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:\Test.XLS" & ";Extended Properties=Excel 8.0")
    cat.ActiveConnection = cnn
    tbl.Name = "MyContacts"
    tbl.ParentCatalog = cat
    tbl.Columns.Append("x")
    cat.Tables.Append(tbl)
    cnn.Close()
End Sub
 
Function getResource(ByVal name As String) As Byte()
    Dim br As New IO.BinaryReader(Me.GetType.Assembly.GetManifestResourceStream(name))
    Dim bytes() As Byte = br.ReadBytes(br.BaseStream.Length)
    br.Close()
    Return bytes
End Function
Some explanations: The interop files is here added as embedded resources, filename f.ex "Interop.ADOX.dll" and is qualified when reading the resource with the local assembly namespace ("WindowsApplication1" here). I found that the COM class type names were appended "Class" when loaded from assembly, like "ADODB.Connection" became "ADODB.ConnectionClass".
 
Try looking into SMO - SQL Server Management Object..... it's the .NET version of ADODX.... and a bit more powerful to boot --- it's similar to SQLDMO from VB6.

-tg
 
That can be done with Assembly.Load method, but you have to create the objects using reflection from the loaded assembly, and late binding (Objects). Here is an example of use where ADOX and ADODB is used to create a new Excel book:
VB.NET:
Sub newexcel()
    Dim ax, adb As System.Reflection.Assembly
    ax = Reflection.Assembly.Load(getResource("WindowsApplication1.Interop.ADOX.dll"))
    adb = Reflection.Assembly.Load(getResource("WindowsApplication1.Interop.ADODB.dll"))
 
    Dim cnn As Object = adb.CreateInstance("ADODB.ConnectionClass") 'ADODB.Connection
    Dim cat As Object = ax.CreateInstance("ADOX.CatalogClass") 'ADOX.Catalog
    Dim tbl As Object = ax.CreateInstance("ADOX.TableClass") 'ADOX.Table
 
    cnn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:\Test.XLS" & ";Extended Properties=Excel 8.0")
    cat.ActiveConnection = cnn
    tbl.Name = "MyContacts"
    tbl.ParentCatalog = cat
    tbl.Columns.Append("x")
    cat.Tables.Append(tbl)
    cnn.Close()
End Sub
 
Function getResource(ByVal name As String) As Byte()
    Dim br As New IO.BinaryReader(Me.GetType.Assembly.GetManifestResourceStream(name))
    Dim bytes() As Byte = br.ReadBytes(br.BaseStream.Length)
    br.Close()
    Return bytes
End Function
Some explanations: The interop files is here added as embedded resources, filename f.ex "Interop.ADOX.dll" and is qualified when reading the resource with the local assembly namespace ("WindowsApplication1" here). I found that the COM class type names were appended "Class" when loaded from assembly, like "ADODB.Connection" became "ADODB.ConnectionClass".

i will defiantly look into this, thanx (i wasnt finding anything like this in my google searches), also this does work in vs 2003? i just need to make sure of this too


Try looking into SMO - SQL Server Management Object..... it's the .NET version of ADODX.... and a bit more powerful to boot --- it's similar to SQLDMO from VB6.

-tg

does this work with Access databases? i may look into this one as well
 
this does work in vs 2003? i just need to make sure of this too
Yes, I wrote the code specially for your platform ;) It is easier with .Net 2.0 and My.Resources.
does this work with Access databases? i may look into this one as well
The namespace is Microsoft.SqlServer.SMO so I think it is for SqlServer only.
 
Yes, I wrote the code specially for your platform ;) It is easier with .Net 2.0 and My.Resources.

I see :)

The namespace is Microsoft.SqlServer.SMO so I think it is for SqlServer only.


i just read up on that this morning here, from the looks of the class i would assume this is for sqlserver and not access
 
MY bad I missed that from the OP there... yes, it is a SQL Server only deal. Sorry about that.... you would think there was something similar in the OLEDBClient namespace though..... opportunity for a third party I suppose.

-tg
 
I would also like to embed a dll into the programs exe file. I am using VS 2005, so this should be easier. The dll is from a control and I don't want to have to distribute it with the exe file
 
ha1fdead, what is easier in .Net 2.0 here is only you access the file bytes from resources through My.Resources.filename. Using this kind of embedded assembly and late binding is not something you do except special cases, it is too much work and bad coding to create the objects and use them through reflection. Look at creating a ClickOnce or MSI setup to deploy your application instead.
 
i've ran into a few snags with this as i'm not familiar with exactly what's going on, here's the code i have so far:

VB.NET:
    Private myADO_Resource As System.Reflection.Assembly = Reflection.Assembly.Load(getResource("LDS_Aggregate.Interop.ADOX.dll"))
    Private AdoxTable As Object = myADO_Resource.CreateInstance("ADOX.TableClass") 'ADOX.Table

    Friend Sub CreateTable()
        'ADOX Table
        AdoxTable.Name = m_strTableName
        Dim AdoxClass As Object = myADO_Resource.CreateInstance("ADOX.ColumnClass") ' ADOX.Column
        With AdoxClass
            .Type = ADOX.DataTypeEnum.adVarWChar
            .Name = "Class"
            .Attributes = ADOX.ColumnAttributesEnum.adColNullable
        End With
        AdoxTable.Columns.Append(AdoxClass)
        Dim AdoxScenario As New ADOX.Column
        With AdoxScenario
            .Type = ADOX.DataTypeEnum.adVarWChar
            .Name = "Scenario"
            .Attributes = ADOX.ColumnAttributesEnum.adColNullable
        End With
        AdoxTable.Columns.Append(AdoxScenario)
    End Sub
now my problems are:
1. AdoxTable.Name = m_strTableName "Option Strict Disallows Late Binding"
2. ADOX.DataTypeEnum.adVarWChar "ADOX is not declared"
3. ADOX.ColumnAttributesEnum.adColNullable "ADOX is not declared"
4. .Name, .Type, .Attributes all are disallowed late binding

i'm starting to think making an installer would be easier than this, anyone got any ideas? keep in mind i can't turn option strict off either so the late binding stuff might cause this route to not work
 
1 & 4 -- you;'ve declared the object as ... well, an object, causing LateBinding. Does it not work as hard-typed to ADOX.TAble.... no I guess not, since it's not recognizing ADOX.....

Scratching my head here.....

-tg
 
If Option Strict Disallows Late Binding you have to turn off option strict to use late binding. For the enum value you have to find out what integer value it represent and define that in your own code. It's late binding for all its worth ;)
 
hmm, perhaps i'll make my own ADOX class that has option strict turned off and i'll use the class to build the DB from scratch that way

either way the whole point to using the ADOX as an embedded resource is to avoid using an installer to distribute the app

also eventually we'll have some excel templates embedded as resources this way too, although we use the Interop Office Assemblies to access the excel files, so office and the interop assemblies do have to be installed already
 
Back
Top