I've created a simple project in VS2022 and created a form which simply tries to open an MS Access database to retrieve data from one of the tables. Here is the code:
private void SurveyYearsForm_Load(object sender, EventArgs e)
{
string connectionString = @Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\....\watch.accdb;
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
connection. Open();
// Do work here.
}
}
It fails on connection. Open() with the Exception "System.Data.Odbc.OdbcException: 'ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified'"
I've created a Data Source and in Server Explorer it successfully creates a connection and I can retrieve data from any of the tables. So I've gone to the Connection String property for the connection and copied the value into the code above but that generates an error so the code must be doing something subtly different to the VS IDE.
The MS Access database was created with Microsoft® Access® 2021 MSO (Version 2401 Build 16.0.17231.20194) 64-bit.
I've looked at the ODBC data Source Administrator (64 bit) and that contains an entry for ACEODBC.DLL version 16.00.17231.20036 so the version number differs but is that material in this case?
I guess some component isn't installed correctly or missing but it's odd (to me at least) that the IDE works.
Any assistance would be gratefully received!



Ok, first up is the issue of the project build as x32 bits, or x64 bits.
Since the Access data engine is going to be x32 bits (in most cases), then you have to force your project build to match the given bit size of the Access data engine.
That is this setting:
So, your project bit size build has to match the Access data engine bit size you installed. Keep in mind that vs2022 is the FIRST version of VS that is x64 bits. Thus, if you use ANY CPU and hit f5, then your project runs as a in-process x64 bit process. In previous versions of VS, hitting F5 with ANY CPU would result in a x32 bit running version of your software.
As noted, you are free to use the ODBC provider, or the oleDB provider. Most would suggest using oleDB providers when working with Access databases, and you do have somewhat better support for some Access database features.
However, ODBC is also a fine choice.
So, next up would be to create a connection string for the whole project (so you don't have to type it over and over in code).
I should also note that while you can use the VS connection wizards to build a valid connection string, the test connection button will NEVER work in vs2022 when running Access x32 bits. And this use case was reversed for versions prior to vs2022.
So, in vs2022, if you running Access x64, then the test connection button will work.
If you are running Access x32, then the test connection will NOT work, however, when you run your code, such connections should work just fine (assuming you forced the project to run as x32 bits, and don't use ANY CPU).
So, I'm going to recommend the oleDB provider, and the reason is the connection builders will work FAR better then ODBC. As noted, ODBC has much merit here, and is a still a good choice, since as noted, you can change that connection string to SQL server, or MySQL or whatever, and you don't have to change the database provider.
So, let's setup an oleDB connection to the Access database.
And might as well use project settings, and thus we don't have to type or see or have the messy connection string in code.
So, open up your project settings.
Hence this:
Note that you VERY much want to check which version of the database engine you are using (you want to use ACE, since that is required to open accDB files, and also that ACE has a x64 bit version available, and JET does not).
So, when building the connection, make 100%, 200%, 300% sure you set or look at which data engine was chosen here (ACE or JET).
Hence hit the advanced tab after doing above, and check that you chosen ACE:
Now, for a test, say a blank new form, drop in a button, drop in a DataGridView.
And thus, now our code becomes this:
And the result is this:
So, in summary:
The test connection for access x32 data engine will NEVER work in vs2022, but hitting F5 to run the project should work. This assumes you set ANY CPU to x32 bits (x86).
However, you can also consider installing Access x64 bits and then force your project to x64 bits. The added bonus is that the test connection button will work in vs2022, but I can't say this is all that large of an issue.
If you goal is ODBC, then I can post a working Access connection for ODBC.