OleDb CreatInstance

Anwind

Well-known member
Joined
Nov 6, 2021
Messages
48
Programming Experience
1-3
Hello.
In System.Data.OleDb,
How do I do Instance about is System.Data.OleDb.OleDbFactory ???
 
Last edited:
If you're targeting .NET Framework then all the OleDb types are in System.Data.dll, which is referenced by default in most projects. If you're targeting .NET Core (.NET 5 and later are based on .NET Core)) then you need to install the System.Data.OleDb NuGet package. Non-standard types are pretty much always added via NuGet in .NET Core projects. As always, you should read the relevant documentation. The documentation for every type tells you what namespace it's a member of and what assembly it's declared in. The .NET Framework 4.8 version is here:


and the .NET 7 version is here:


Those are Australian pages, so you can change the URL for your own culture, if it's available.

Note that OleDb is part of the Platform Extensions and not part of .NET Core itself because it is Windows-specific.
 
If you're interested in using ADO.NET provider factories, you may want to check out this blog post of mine on the subject. It only covers fairly basic stuff but it may be a god place to start.
 
Are you targeting .NET Framework or .NET Core (including .NET 5 or later)? As I stated, the NuGet package is for .NET Core. .NET Framework just requires a reference to System.Data.dll, which is probably there by default.
 
Note that you would only install version 7 of the NuGet package in a project that targeted .NET 7. If you're targeting .NET 6 then you'd install version 6, etc.

One thing I neglected to mention is how you explicitly get an instance of the OleDbFactory class. I was thinking that my blog post would cover it but it doesn't things in a more provider-agnostic way. The class is a singleton, so you get the one and only instance from the class like so:
C#:
var factory = OleDbFactory.Instance;
Note that I have tested that in projects targeting both .NET Framework 4.8 and .NET 6 and it works exactly as expected.
 
Why would it? Where did I suggest that it would? You specifically asked about the OleDbFactory class and I told you how to get an instance of that class. If you want to create an OleDbParameter directly, rather than through the factory, you just use a constructor for that type.
 
*sigh* Read the documentation. Once you have any kind of DbFactory, you can call methods on the factory to create a DbCommand an DbParameter.

 
Back
Top Bottom