This article demonstrates in a step by step fashion the easiest, and frankly fastest way to connect
Step:1
Oracle has an Oracle Database Instant Client which is a set of Dlls which can be XCopy installed onto the development and target PC to allow Oracle database access without installing the full Oracle client. In future steps we will include those target Dlls to be copied to the target output folder with the executable. Download the appropriate package and add Dlls to a folder of your choice on the PC.
Step:2
Update: This step should not be done, for the dlls will end up being copied into a subfolder of the same name and when running the client an error may come up stating “System.Data.OracleClient requires Oracle client software
version 8.1.7 or greater”.
Step:3
Add the reference to the project of System.Data.OracleClient to the project.
Step:4
In Studio again (highlight the project root) and from the right click menu select Add->Existing Item, and insert all the top level Oracle Dlls from step one into the folder; unless for some reason you need previous versions all those dlls at the top level should be fine.
Step:5
Highlight all the inserted DLLs and select Properties to bring up the properties window. Change the Build Action to be Content and the Copy To Output Directory to be Copy If Newer. This allows the dlls to reside with the created executable program. By doing this it allows the program to run on another computer, as well as this one, that does not have the Oracle Client installed because all the Oracle specific dlls reside with the output executable.
Step:6
On the design view of the form add a button, label, binding source and a DataGridView. The names used for each in the example (Label as lbState, Binding Source as bsOracle and DataGridView as gvOracle).
Step:7
Create in the forms code a method to handle the connection string and add the target Oracle db/instance items (Note: Replace { xxx } including the curly braces the specifics to your db)
Code
private string GenerateConnectionString()
{
return "Data Source=( DESCRIPTION = ( ADDRESS_LIST = ( ADDRESS = ( PROTOCOL = TCP )( HOST = {Insert Host Here} )( PORT = {Insert Port Here} ) ) )( CONNECT_DATA = ( SERVER = DEDICATED )( SERVICE_NAME = {Service Name Here } ) ) ); User Id= {DB ID Here}; Password = {Password Here};";
}
Step:8
------
In the button’s onclick event wire up the controls and access the Oracle database as such:
try
02.{
03. using ( OracleConnection connection = new OracleConnection( GenerateConnectionString() ) )
04. {
05. connection.Open();
06. lblState.Text = connection.State.ToString();
07.
08. OracleCommand oc = connection.CreateCommand();
09. oc.CommandText = "SELECT * FROM {Your Table Here}";
10.
11. OracleDataReader reader = oc.ExecuteReader();
12.
13. bsOracle.DataSource = reader;
14. gvOracle.DataSource = bsOracle;
15.
16. gvOracle.BorderStyle = BorderStyle.Fixed3D;
17. gvOracle.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
18.
19. }
20.}
21.catch ( Exception ex )
22.{
23.// MessageBox.Show( "Exception: " + ex.Message );
24. lblState.Text = ex.Message;
25.}
Step:9
---------
Run and compile the program and if all goes well it should attach…but there are other possible failure points see the next section as to a couple of them.