0

In Visual Studio I'm able to simply add a reference to an assembly, for example: Interop.ADODB.

How do I do this in powershell? The following is exploding

[Reflection.Assembly]::LoadWithPartialName("Interop.ADODB")

$conn = New-Object ADODB.Connection

And here is the error message

New-Object : Constructor not found. Cannot find an appropriate constructor for type ADODB.Connection.
At C:\Users\michaelr\Desktop\jet-project\PS\test.ps1:3 char:19
+ $conn = New-Object <<<<  ADODB.Connection
    + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

I'm on Windows 7 x64.

3
  • Please include the error message that PowerShell gives you back. Also, what version of Windows (including 32/64 bit) and PowerShell are you using? Commented Jun 27, 2013 at 19:17
  • What are you trying to connect to? Depending on the data source a better option may be to use native .net provider instead of adodb COM. Commented Jun 27, 2013 at 23:53
  • I'm pulling out the 'Jet OLEDB:Engine Type' property from a .mdb file (JET DB), which cannot be done with the native .net providers. Commented Jun 28, 2013 at 10:20

3 Answers 3

2

Use this line instead:

$conn = New-Object -comobject ADODB.Connection

Powershell understand com object natively (well to the extent it's possible for objects that don't always have a lot of metadata) and does all the interop for you. You just need to tell powershell that it's a COM object. You don't need to reference the interop assemblies explicitly.

Having said that, depending on your task you might be better off using a native .net provider instead of ADODB.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that appears to have fixed the issue. I'm now running into another problem, I'm using the following in the call to open: "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Users/bubba/Desktop/jet-project/Access 2000 Database.mdb", but it's coming backw ith an error about the provider not being found. I have a small C# project doing the exact same thing successfully.
And to explain why I'm using ADODB, it's because I'm trying to access the properties collection, which isn't exposed from the newer solutions.
I figured out the issue, the Microsoft.Jet.OLEDB.4.0 provider is only available in a 32-bit context. Now that I've fixed that, it's working beautifully :)
0

You're looking for the Add-Type cmdlet, which lets you either compile .NET code into an in-memory assembly, or load an assembly from disk.

Check out the -AssemblyName parameter if you want to load an assembly from the GAC, or the -Path parameter if you know exactly where the DLL you want to load is.

2 Comments

The line: 'Add-Type -AssemblyName Interop.ADODB.dll' resulted in a 'could not be found' error.
If you're loading an assembly from the GAC, you need to use its strong name, not its file name. For example, you'd use "Add-Type -AssemblyName ADODB" to load that DLL from the GAC. Here's a page that tries to explain how Add-Type and GAC-based assemblies work: sqlservercentral.com/blogs/kyle-neier/2012/06/06/… That said, I forgot to consider whether or not ADODB could be accessed through COM, and since it can, Zespri's answer is probably the best one for this particular instance.
0

The answer's right in front of you:

Cannot find an appropriate constructor for type ADODB.Connection

You need to pass the connection string to the constructor like this:

$c = new-object ADODB.connection "server=foo;user id=bar ..."

However, I don't understand why you want to use 1998-era ADODB in .NET when you can use System.Data instead. Check out this MSDN article on accessing databases with PowerShell:

http://technet.microsoft.com/en-us/magazine/hh855069.aspx

1 Comment

The obvious guess would be that I'm interfacing with '1998-era' data in such a way that the new tools don't allow me to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.