Using Microsoft .NET and C# with Oracle 9i - Connecting to a Database
(Page 2 of 4 )
The process for connecting to an Oracle database is very straightforward, especially for anyone who has worked with the .NET database connection classes for SQL Server. Oracle basically copied the same class structure that Microsoft used in the SQL Server connection classes already present in .NET. This makes moving over from SQL Server to Oracle relatively easy. It also means that a lot of examples describing database interaction with SQL Server can easily be modified to work with Oracle. In any case, here is a quick overview of the steps required to connect to an Oracle database.
When working with a database, we must create an OracleConnection object to retain all information for communicating with the database. First, we import the required assemblies.
using Oracle.DataAccess.Client;
using System.Data;
Next, we create the connection string with the user credentials information. In this case, the Oracle schema is user1 the password is mypass and the Oracle database name either in the tnsnames file or in the Oracle Internet Directory is db.
String connString = “User id=user1;Password=mypass;source=db”;
Now, the connection object, conn, is created using the connection string.
OracleConnection conn = new OracleConnection(connString);
The last step is to begin communication with the database using the Open() method.
conn.Open();
This creates a connection to a database and opens it. The most important part is making sure the connection string is correct. Each different type of database uses a different connection string and the differences in syntax can be a problem if you don’t pay close attention to them.
Next: Creating a Command >>
More .NET Articles
More By Michael Swannson