Click here to Skip to main content
Click here to Skip to main content

Dynamically adding DSN-names

, 6 Jul 2004
Rate this:
Please Sign up or sign in to vote.
A simple class to dynamically add DSN-names to your system.


First, let me start by saying that normally I use a OLEDB-connection to my database. But after coming across a bug in the .NET Framework (on a European, in this case Dutch, Windows system, the decimal point is lost due to the different symbol), I had no other choice than to use ODBC. Now there is no problem with ODBC, it works fine. Except,... those damn DSN-names. Not wanting to add the DSN-names myself, I surfed the Net and found... well, virtually nothing. After finding some hints in a Microsoft forum, I decided to write this article on using an API-call to dynamically create DSN-names.

Building the class

Import API

In order to import the ODBCCP32.dll which contains our API-call, we have to include the InteropServices namespace.

using System.Runtime.InteropServices;

Now it is time to import the DLL and create our property:

private static extern bool SQLConfigDataSource(
    IntPtr parent, int request, string driver, string attributes);

The parent parameter refers to the parent window handle and can be set to 0. The request parameter is a number from 1 to 6 and refers to the requested action. This is a list of the values and their actions:

  1. ODBC_ADD_DSN (use this to add a user DSN)
  2. ODBC_CONFIG_DSN (use this to configure a user DSN)
  3. ODBC_REMOVE_DSN (use this to remove a user DSN)
  4. ODBC_ADD_SYS_DSN (use this to add a system DSN)
  5. ODBC_CONFIG_SYS_DSN (use this to configure a system DSN)
  6. ODBC_REMOVE_SYS_DSN (use this to remove a system DSN)

The driver parameter is the name of the driver, and the attributes parameter contains the database path and other login information.

Creating our methods

Now, it is time to create easy to use methods. Let us say that we always connect to a Microsoft Access database (for now). In that case, we only have to provide the DSN-name and database path. In this case, our AddUserDSN method will be:

public bool AddUserDSN(string DSName, string DBPath)
  return SQLConfigDataSource((IntPtr)0, 1, 
     "Microsoft Access Driver (*.MDB)\0",
     "DSN=" + DSName + "\0Uid=Admin\0pwd=\0DBQ=" + DBPath + "\0");

The method will return true if the action is successful. We now can call our method using the following syntax:

DSNAdmin.AddUserDSN("MyDSNName", "C:\MyDBPath\MyDB.mdb");

No more messing around in the configuration screen, users can now change their DB path without having to worry about changing DSN-properties.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Bas Goedemans
Web Developer
Netherlands Netherlands
I am a programmer at a software company that creates software for training companies. (

Comments and Discussions

GeneralMy vote of 5 PinmemberMember 28714320-Jul-10 20:19 
GeneralAdd to a remote computer Pinmembervincedr22-Aug-06 4:54 
QuestionRe: Add to a remote computer PinmemberOgnjenk22-Dec-06 0:07 
GeneralVB.Net Version with Corrected Type Declarations Pinmemberatomicfroman8-Aug-06 10:43 
Generalreading data from a CSV file. PinmemberBhargavaJK29-Jun-06 23:20 
Questionhow to show DSN box dynamically Pinmembervytheeswaran27-Jan-06 20:40 
GeneralProblem in Microsoft Text Driver Pinmembersiva_218117-Apr-05 19:58 
GeneralRe: Problem in Microsoft Text Driver PinmemberAlexandros20051020-Jan-06 2:48 
Generalsystem dsn for sql server PinsussSytem DSN for SQL Server30-Mar-05 10:44 
GeneralRe: system dsn for sql server PinmemberBas Goedemans30-Mar-05 23:01 
GeneralRe: system dsn for sql server PinsussSytem DSN for SQL Server31-Mar-05 12:29 
GeneralRe: system dsn for sql server PinmemberMike Ballou6-Jun-05 11:26 
GeneralRe: system dsn for sql server PinmemberronyVain2-Nov-06 1:59 
GeneralRe: system dsn for sql server Pinmemberjp_netvision22-Nov-05 22:48 
GeneralSystem dsn for sql server UID and PWD issue Pinmembercpg8329-May-06 23:00 
GeneralSorry, nobody can help you PinmemberAndraxx13-Jul-07 8:57 
GeneralRe: Sorry, nobody can help you Pinmembercskanth18-Sep-07 10:25 
GeneralRe: system dsn for sql server Pinmembertania_karenina29-Jan-07 14:57 
GeneralNeed Help PinsussDerek Smigelski19-Oct-04 6:32 
GeneralRe: Need Help PinmemberBas Goedemans20-Oct-04 0:45 
GeneralRe: Need Help Pinmemberbits_625-Nov-06 20:32 
QuestionSame with registry? PinmemberLars [Large] Werner7-Jul-04 11:48 
AnswerRe: Same with registry? PinmemberBas Goedemans8-Jul-04 1:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150301.1 | Last Updated 7 Jul 2004
Article Copyright 2004 by Bas Goedemans
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid