//copy database files to target directory
string fileName = "test.txt";
string targetPath = StaticFormInstances.choose.txtbxTargetDir.Text;
string sourcePath = @"\\il93fil48\3GSM\release\testapplication_REL_01_00_09\database";
//Use Path Class to manipulate file and directory paths
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
//Create a new target folder if it doesn't exist
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
//calculate total number of files in source
int totalNumberOfFiles = files.Length;
//current file number
int fileNumber = 0;
//create object to store database string
OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();
foreach (string s in files)
{
//increase file number by 1
fileNumber++;
//copy file based on fileNumber
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, false);
//set up database
builder.Driver = "Microsoft Access Driver (*.mdb)";
builder.Dsn = fileName.Substring(0,fileName.Length - 4);
builder["Description"] = fileName.Substring(0, fileName.Length - 4);
builder["DBQ"] = destFile;
Console.WriteLine(builder.ConnectionString);
Console.WriteLine();
//clear current values and reset know keys to default
builder.Clear();
//calculate progess out of base of 100
int ProgressPercentage = (fileNumber / totalNumberOfFiles) * 100;
//update the progress bar
bckgrdSetUpDatabases.ReportProgress(ProgressPercentage);
}
} lotrsimp12345 37 Posting Pro in Training
Dani AI
Generated
— short diagnosis and a practical checklist.
What your code is doing vs. what the ODBC administrator shows: building a connection string with OdbcConnectionStringBuilder only prepares the string you pass to the ODBC Driver Manager; it does not register a DSN in Windows. If you set a Dsn key on the builder you are only inserting that name into the string — you are not creating a machine/user DSN that the ODBC Data Source Administrator will list. The IM002-style error you saw usually means the Driver Manager could not find the named DSN or a usable driver when the connection was attempted. (learn.microsoft.com)
Common causes (and why the ODBC admin appears unchanged)
- You never created a System/User DSN — programmatically building a connection string won’t add one to the registry.
- Driver/architecture mismatch: 32-bit vs 64-bit drivers and tools are separate on Windows; using the wrong ODBC admin or installing the wrong-bit driver leads to “DSN not found” or similar errors. Use the SysWOW64/odbcad32.exe tool for 32-bit DSNs on 64-bit Windows. (support.microsoft.com)
How to fix it (practical options)
- Easiest: use a DSN-less connection string (pass Driver/Dbq or provider and file path directly) so no DSN needs to be created or visible. This avoids touching the ODBC administrator. (support.microsoft.com)
- If you must create a DSN: call the ODBC setup API (SQLConfigDataSource) or create the correct registry keys for the appropriate hive (HKLM for System DSN, HKCU for User DSN). Be mindful of UAC and process bitness when creating system DSNs. (learn.microsoft.com)
- For Access files consider the OLE DB/JET or ACE providers instead of ODBC (install the matching ACE engine for the bitness you need). Many Access/ACE errors are simply bitness/provider-install issues. (learn.microsoft.com)
Quick troubleshooting checklist
- Try a manual test DSN in the correct ODBC admin (
%windir%\SysWOW64\odbcad32.exefor 32-bit). - Confirm the exact driver name your connection string should use and that the driver is installed for the process bitness.
- If creating DSNs from code, run the creation step elevated and with the correct architecture or use SQLConfigDataSource.
- Prefer DSN-less strings for simple file-based Access connections to avoid registry/DSN headaches.
This explains why the ODBC Data Source Administrator window does not show changes when your code runs and gives practical next steps to get a connection working. (learn.microsoft.com)
lotrsimp12345 37 Posting Pro in Training
Updated code
Following error: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
//set up database
OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();
builder.Driver = "Microsoft Access Driver (*.mdb)";
builder.Dsn = fileName.Substring(0, fileName.Length - 4);
builder["Description"] = fileName.Substring(0, fileName.Length - 4);
builder["DBQ"] = destFile;
System.Data.Odbc.OdbcConnection conn = new OdbcConnection(builder.ToString());
try
{
conn.Open();
// Process data here.
}
catch (Exception ex)
{
MessageBox.Show("Failed to connect to data source" + ex);
} Edited by lotrsimp12345 because: n/a
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.