Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

Introduction

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 ODBC_PATH = "SOFTWARE\\ODBC\\ODBC.INI\\";
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.

License

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

Share

About the Author

Vipin_Arora
Software Developer (Senior)
India India
Linkedin profile: http://www.linkedin.com/profile/view?id=241442098
Follow on   LinkedIn

Comments and Discussions

 
Questionvote 5 Pinmemberbeqiraj2542-Apr-12 3:08 
AnswerRe: vote 5 [modified] Pinmemberwipin (Vipin Arora)2-Apr-12 21:27 

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 | Mobile
Web04 | 2.8.140921.1 | Last Updated 30 Mar 2012
Article Copyright 2012 by Vipin_Arora
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid