Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#
Article

Dynamically adding DSN-names

Rate me:
Please Sign up or sign in to vote.
3.90/5 (15 votes)
6 Jul 20042 min read 153.8K   2K   51   23
A simple class to dynamically add DSN-names to your system.

Introduction

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.

C#
using System.Runtime.InteropServices;

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

C#
[
DLLImport("ODBCCP32.dll")
]
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:

C#
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:

C#
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.

License

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


Written By
Web Developer
Netherlands Netherlands
I am a programmer at a software company that creates software for training companies. (www.ksdgroep.nl)

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 28714320-Jul-10 19:19
Member 28714320-Jul-10 19:19 
GeneralAdd to a remote computer Pin
vincedr22-Aug-06 3:54
vincedr22-Aug-06 3:54 
QuestionRe: Add to a remote computer Pin
Ognjenk21-Dec-06 23:07
Ognjenk21-Dec-06 23:07 
GeneralVB.Net Version with Corrected Type Declarations Pin
atomicfroman8-Aug-06 9:43
atomicfroman8-Aug-06 9:43 
Generalreading data from a CSV file. Pin
BhargavaJK29-Jun-06 22:20
BhargavaJK29-Jun-06 22:20 
Questionhow to show DSN box dynamically Pin
vytheese27-Jan-06 19:40
professionalvytheese27-Jan-06 19:40 
GeneralProblem in Microsoft Text Driver Pin
Shiv...17-Apr-05 18:58
Shiv...17-Apr-05 18:58 
GeneralRe: Problem in Microsoft Text Driver Pin
Alexandros20051020-Jan-06 1:48
Alexandros20051020-Jan-06 1:48 
Generalsystem dsn for sql server Pin
bhavnachauhan30-Mar-05 9:44
bhavnachauhan30-Mar-05 9:44 
GeneralRe: system dsn for sql server Pin
Bas Goedemans30-Mar-05 22:01
Bas Goedemans30-Mar-05 22:01 
GeneralRe: system dsn for sql server Pin
bhavnachauhan31-Mar-05 11:29
bhavnachauhan31-Mar-05 11:29 
This is what I have:
SQLConfigDataSource((IntPtr)0, 4, "SQL Server", "DSN=Test\0Description=Test\0Network=DBMSSOCN\0Trusted_Connection=No\0UID=sa\0PWD=pwd\0Server=serverName\0Database=SqlCollectionDB\0");
This is for creating the data source. This fails to create the dsn i.e. doesn't create it at all.
When I use Trusted_Connection=Yes it works fine. If I use Trusted_Connection=No but don't specify the UID and PWD --then it creates the dsn with sql auth but leaves the user and pwd blank because i did not specify it.
registry entries for ODBC.INI does not store the registry value for password--so I am not sure how I can go about using the registry settings for this.

Thanks
GeneralRe: system dsn for sql server Pin
Member 9276906-Jun-05 10:26
Member 9276906-Jun-05 10:26 
GeneralRe: system dsn for sql server Pin
ronyVain2-Nov-06 0:59
ronyVain2-Nov-06 0:59 
GeneralRe: system dsn for sql server Pin
jp_netvision22-Nov-05 21:48
jp_netvision22-Nov-05 21:48 
GeneralSystem dsn for sql server UID and PWD issue Pin
cpg8329-May-06 22:00
cpg8329-May-06 22:00 
GeneralSorry, nobody can help you Pin
Andraxx13-Jul-07 7:57
Andraxx13-Jul-07 7:57 
GeneralRe: Sorry, nobody can help you Pin
cskanth18-Sep-07 9:25
cskanth18-Sep-07 9:25 
GeneralRe: system dsn for sql server Pin
tania_karenina29-Jan-07 13:57
tania_karenina29-Jan-07 13:57 
GeneralNeed Help Pin
Derek Smigelski19-Oct-04 5:32
Derek Smigelski19-Oct-04 5:32 
GeneralRe: Need Help Pin
Bas Goedemans19-Oct-04 23:45
Bas Goedemans19-Oct-04 23:45 
GeneralRe: Need Help Pin
bits_625-Nov-06 19:32
bits_625-Nov-06 19:32 
QuestionSame with registry? Pin
Lars [Large] Werner7-Jul-04 10:48
professionalLars [Large] Werner7-Jul-04 10:48 
AnswerRe: Same with registry? Pin
Bas Goedemans8-Jul-04 0:52
Bas Goedemans8-Jul-04 0:52 

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

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