Showing posts with label Oracle. Show all posts
Showing posts with label Oracle. Show all posts

Saturday, 3 December 2011

configuring php to connect with oracle database using XAMPP server manager



[caption id="" align="alignright" width="190" caption="Image via CrunchBase"]Image representing Windows as depicted in Crun...S[/caption]

Today i was searching for "how to configure php to connect with oracle ".  I tried different search words and searched the whole internet. Finally i was successful.

I use the package XAMM for windows to run PHP and MySql.

Step-A


Check whether you have oracle client already installed

  1. In your XAMPP Start Page, go to phpinfo, look for string oci8. If string found it indicate that connection to oracle is available and simply execute Step-3.           Otherwise to activate connection do the following steps:

  2. Open the currently used php.ini file by looking at the phpinfo, from the XAMPP folder.

  3. Find string ;extension=php_oci8.dll. Remove the semicolon (;) from the begining of the string to activate the oracle extension.

  4. Save the php.ini file.

  5. Download the “Instant Client Package – Basic” for Windows from the OTN Instant Client page. Unzip it to "C:\xampp\php\ext" folder

  6. Edit the PATH environment setting and add C:\xampp\php\ext before any other Oracle directories. For example, on Windows XP, follow Start -> Control Panel -> System -> Advanced -> Environment Variables and edit PATH in the System variables list.

  7. Set desired Oracle globalization language environment variables such as NLS_LANG. If nothing is set, a default local environment will be assumed. See An Overview on Globalizing Oracle PHP Applications for more details.

  8. Unset Oracle variables such as ORACLE_HOME and ORACLE_SID, which are unnecessary with Instant Client (if they are set previously).

  9. Restart XAMPP (or Start if its not already started).

  10. To make sure that connection to oracle database has successfully activated, go to phpinfo. Find string: oci8. If found, then XAMPP can now communicate with Oracle Database.


Step-B


Now connect to your desired database and execute your query

<?php

define('DB_SERVER', 'yourservername');
define('DB_USERNAME', 'yourUserName');
define('DB_PASSWORD', 'yourPassword');
define('DB_DATABASE', 'yourDatabaseName');
$conn= oci_connect(DB_USERNAME, DB_PASSWORD, DB_SERVER)
or die(oci_error());
echo "success...";
//$database = oci_select_db(DB_DATABASE) or die(oci_error());
$stid = oci_parse($conn, 'SELECT * FROM employees'); oci_execute($stid); echo "<table border='1'>\n"; while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {     echo "<tr>\n";     foreach ($row as $item) {         echo "    <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";     }     echo "</tr>\n"; } echo "</table>\n";
?>

For further help you can always refer the PHP manual.
Enhanced by Zemanta

Monday, 2 May 2011

C#: Connect To Oracle Database With No Oracle Client Install Needed (Winform DataGridView Loading Example)

Oracle_smallThis article demonstrates in a step by step fashion the easiest, and frankly fastest way to connect to an Oracle database using C#. The goal is to not have to install the huge Oracle Client either on the development machine nor the target machine the code will run on. This example creates a winform and inserts the content into a DataGridView for quick viewing. The code below is base on .Net 3.5.

Steps



  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.

  2. In Visual Studio create a Winform Project. From the Solution Explorer and within the project create a subfolder named Oracle Dlls. 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”.

  3. Add the reference to the project of System.Data.OracleClient to the project..

  4. In Studio again select the folder created in step 2 (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 directory into the folder project. Note the below picture shows into a folder, they do not go into a folder but at the root of the project.
    OracleFolder2

  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.

  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).

  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) :











    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};";

    }




  8. In the button’s onclick event wire up the controls and access the Oracle database as such:











    try

    {

        using ( OracleConnection connection = new OracleConnection( GenerateConnectionString() ) )

        {

            connection.Open();

            lblState.Text = connection.State.ToString();


            OracleCommand oc = connection.CreateCommand();

            oc.CommandText = "SELECT * FROM {Your Table Here}";


            OracleDataReader reader = oc.ExecuteReader();


            bsOracle.DataSource = reader;

            gvOracle.DataSource = bsOracle;


            gvOracle.BorderStyle = BorderStyle.Fixed3D;

            gvOracle.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;


        }

    }

    catch ( Exception ex )

    {

    //  MessageBox.Show( "Exception: " + ex.Message );

        lblState.Text = ex.Message;

    }




  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.


Note for a more involved explanation of this process check out the article Instant Oracle Using C# which has a console example and does not provide the advice of using the build options in Studio for the Oracle Dlls. That will be our little secret. HTH

Possible Errors Encountered


System.Data.OracleClient requires Oracle client software version 8.1.7 or greater

If this is encountered it could be a permission problem accessing the location where the oracle dlls are…but most likely the dlls are not in the same directory as the executable or found within the environment path of the system.

ORA-12541: TNS:no listener

This one could mean that one of the connection settings is incorrect and the database could not be connected to and this generic error comes back. Try tweaking the settings, port, instance

Sunday, 1 May 2011

System.Data.OracleClient requires Oracle client software version 8.1.7 or greater.

If the following error occurs, while running ASP.NET web application in IIS 6.0,

Follow the bellow instructions

Step - 1 )  Un-install any sql client installed

Step- 2 )  Re-install it.

Reason: Sometimes the web server can not collect those drivers needed fot ASP.NET Framework (if installed after installing the Database client)