Code example - Connecting to Oracle and running a simple query
Once you have the above, the rest is easy.
Create a new C# application. For this example, let's keep it simple and create it as a console application. Be sure to include a reference to System.Data.OracleClient.dll, and place the following at the top of your code along with all the other using statements: Collapse | Copy Code using System.Data.OracleClient; This is a standard library provided by Microsoft. No voodoo witchcraft or additional Oracle library references are required. More information about this library can be found here. The following section of code should be all you need to get yourself started. This is simply an exercise in connecting to the database and running a simple SELECT query to return some data. The purpose of this article is to establish a connection to Oracle, installing as little as possible on a user's machine. You won't be seeing anything more complicated than that. We can save the rest for another article. We will start by creating two methods: static private string GetConnectionString() and static private void ConnectAndQuery(). I won't be going into any specific details regarding any of the code provided. There's plenty of documentation available to explain what can be done withSystem.Data.OracleClient, if you want more information. Collapse | Copy Code // This really didn't need to be in its own method, but it makes it easier // to make changes if you want to try different things such as // promting the user for credentials, etc. static private string GetConnectionString() { // To avoid storing the connection string in your code, // you can retrieve it from a configuration file. return "Data Source=myserver.server.com;Persist Security Info=True;" + "User ID=myUserID;Password=myPassword;Unicode=True"; }
// This will open the connection and query the database static private void ConnectAndQuery() { string connectionString = GetConnectionString(); using (OracleConnection connection = new OracleConnection()) { connection.ConnectionString = connectionString; connection.Open(); Console.WriteLine("State: {0}", connection.State); Console.WriteLine("ConnectionString: {0}", connection.ConnectionString);
OracleDataReader reader = command.ExecuteReader(); while (reader.Read()) { string myField = (string)reader["MYFIELD"]; Console.WriteLine(myField); } } } I will assume you can make the necessary modifications to the connection string and your query. The code should otherwise be self-explanatory. All that remains is a call to ConnectAndQuery() from Main. Errors you may run into at runtime Collapse | Copy Code Error: Unhandled Exception: System.Data.OracleClient.OracleException: ORA-12154: TNS:could not resolve the connect identifier specified Cause: Your connection string is invalid. Missing the tsanames.ora file. Resolution: Fix the connection string making sure the server name and/or credentials are correct. Make sure the tsanames.ora file is present in the application path and contains valid data. Collapse | Copy Code Error: Unhandled Exception: System.Exception: OCIEnvNlsCreate failed with return code - 1 but error message text was not available. Cause: One or both of the required Oracle Instant Client DLLs are missing from the application's path. There is no 'PATH=' environmental variable set that points to these files should they not reside in the application's path. Resolution: Copy the DLLs into the application's path or modify your PATH= to include the directory where these files reside. Collapse | Copy Code Error: Unhandled Exception: System.Data.OracleClient.OracleException: ORA-12705: Cannot access NLS data files or invalid environment specified Cause: You have Oracle or Oracle development tools installed locally (or on the machine running the application). Resolution: Check to see if [HKLM/Software/Oracle] exists. Chances are it does. Within the Oracle key, look to see if NLS_LANG exists. If it does, do one of the following: rename to NLS_LANG.OLD, or delete it entirely. Providing a valid language such as AMERICAN_AMERICA.WE8MSWIN1252 would also resolve the issue. Single-byte character sets include US7ASCII, WE8DEC, WE8MSWIN1252, and WE8ISO8859P1. Unicode character sets include UTF8,AL16UTF16, and AL32UTF8.
ASP. Net Sample code for instant Oracle Connect: <%@ Page Language="VB" %> <%@ Import Namespace="Oracle.DataAccess.Client" %> <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim cn As OracleConnection = New OracleConnection() Dim strconn As String
'If you do set up tnsnames.ora , you can use the following code: 'strconn = "user id=your_userid;password=your_password;data source=your_oracle_server/your_oracle_service"