Click here to Skip to main content
13,589,923 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 23 Mar 2012
Licenced CPOL

Create an Oracle DSN in C#

, 29 Mar 2012
Rate this:
Please Sign up or sign in to vote.
Create an Oracle Data Source Name programmatically.


This tip is about creating an Oracle DSN using C#.

Using the code

In order to create an Oracle DSN in C#, we just need to write a few lines of code. Consider the code below::

string driverName = "Oracle in OraClient10g_home1";
string dsnName = "OracleFromCode";
string database = "MyDatabase";
string description = "This DSN was created from code!";
string server = "ORCL"; //TSN Name

string driverPath = @"C:\Oracle\product\10.1.0\Client_1\BIN\SQORA32.DLL";

// value to odbc data source         
var datasourcesKey = Registry.LocalMachine.CreateSubKey(ODBC_PATH + "ODBC Data Sources");
if (datasourcesKey == null) 
throw new Exception("ODBC Registry key does not exist!!");
datasourcesKey.SetValue(dsnName, driverName);

//It will Create new key in odbc.ini         
var dsnKey = Registry.LocalMachine.CreateSubKey(ODBC_PATH + dsnName);
if (dsnKey == null) 
throw new Exception("ODBC Registry key not created!!");
dsnKey.SetValue("Database", database);
dsnKey.SetValue("Description", description);
dsnKey.SetValue("Driver", driverPath);
dsnKey.SetValue("Server", server);
dsnKey.SetValue("Database", database);
dsnKey.SetValue("userID", "DBUSER");
dsnKey.SetValue("password", "DBPWD");

In the above code, I have hardcoded some values. See below comments with code for these values:

string driverName = "Oracle in OraClient10g_home1"; //Name of your Oracle Driver
string dsnName = "OracleFromCode";//Name of your DSN
string database = "MyDatabase";//Name of the Database or Tablespace
string description = "This DSN was created from code!";//Description (Optional)
string server = "ORCL"; //TSN Name of the oracle

Next, you need to pass the driver (SQORA32.DLL).

At last, pass all these values to "SetValue" (see last few lines of code). I have passed the database username and password hardcoded. You can also pass these through user inputs.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
Linkedin profile:

You may also be interested in...


Comments and Discussions

Questionvote 5 Pin
beqiraj2542-Apr-12 3:08
memberbeqiraj2542-Apr-12 3:08 
AnswerRe: vote 5 Pin
wipin (Vipin Arora)2-Apr-12 21:27
memberwipin (Vipin Arora)2-Apr-12 21:27 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180618.1 | Last Updated 30 Mar 2012
Article Copyright 2012 by Vipin_Arora
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid