Wednesday, 19 December 2012

C to Oracle database connectivity

 Hello Friends,
                          Following information will give you step wise execution of the C program with oracle database in visual studio.


Open the Visual Studio ..

1] Creating new project
  •  Create new project .. 
  •  Select Win32 Console application. 
  •  Give the appropriate name.

2]Click on next


3]Select the Console application.

       Please uncheck the Precompiled headers.

  •      Click on Empty project


  •  Click on finish .. 
  •  It will create new project with empty folders in it 
  •  Add the new Item in source directory rename it as test.c or any *.c name.

Database Connection

1]Connect to database
  •   Go to the Tools 
  •   connect to the database option



2]Connect to the Data source Oracle..



3] Add connection

  •  Server name :your SID 
  •  Username :oracle username (system) 
  •  Password :oracle password(system)




4]Test the connection..
.

If it succeeded then do the further steps..or else you have problem in your database username/password or in SID


Project properties.. 
1]Right click on project 
  •  Choose option c/c++ 
  •  Add the Additional Directories.. 
                Here give the path upto include folder where your oracle is installed.




2]Go to the linker..and select the general option

 Add two path Additional libraries as :-
  • D:\oracle\product\10.2.0\db_1\OCI\lib;
  • D:\oracle\product\10.2.0\db_1\OCI\lib\MSVC"





3]select the input option

Add additional dependency as following path

  • D:\oracle\product\10.2.0\db_1\OCI\lib\MSVC\oci.lib



------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------


Save the following program as Test.c
       
        #include <stdlib.h>
#include <stdlib.h>
#include <ocidem.h>
#include <oratypes.h>
#include <ocidem.h>   
#define  VERSION_7 2
   
Lda_Def lda;
char hda[256];
Cda_Def cda;
   
void showError(Lda_Def* dataArea) 
         {
   
          char msg[512];   
           oerhms(&lda, dataArea->rc, msg, sizeof(msg));   
          printf("Error code   : %i \n", dataArea->rc);
          printf("Error message: %s \n", msg );   
           exit  (EXIT_FAILURE);   
}
   
  main()
      {
 
  char* sql = "INSERT INTO PEOPLE(NAME, AGE, BORN) VALUES
                              (:NAME, :AGE, TO_DATE(:BORN, 'DD.MM.YY HH24:MI:SS'))";

 char* name = "Apple";
 int   age  = 60;
 char* born = "15.03.1984 18:45:21";
 
 /* CONNECT TO DB */
          if (olog  (&lda, hda, "username", -1, "password", -1, "SID", -1, OCI_LM_DEF))       
           { showError(&lda); }
   
 /* CREATE CURSOR */ 
 if (oopen (&cda, &lda, 0, -1, -1, 0, -1))                            
          { showError(&cda); }
      
 /* PARSE SQL */
 if (oparse(&cda, sql, -1, 0, VERSION_7))                     
          { showError(&cda); }
 
 /* BIND VARIABLES */
 if (obndrv(&cda, ":NAME", -1, name, strlen(name)+1, STRING_TYPE, -1, 0, 0, -1, -1)) 
         { showError(&cda); }
 if (obndrv(&cda, ":AGE" , -1, &age, sizeof(int)   , INT_TYPE   , -1, 0, 0, -1, -1)) 
         { showError(&cda); }
 if (obndrv(&cda, ":BORN", -1, born, strlen(born)+1, STRING_TYPE, -1, 0, 0, -1, -1)) 
         { showError(&cda); }
   
         /* EXECUTE STATEMENT */
 if (oexec (&cda) && cda.rc != 1)                                         { showError(&cda); }
   
 /* COMMIT */
 if (ocom  (&lda))                                                               { showError(&cda); }

         /* CLOSE CURSOR */
 if (oclose(&cda))                                                                   { showError(&lda); } 

         /* CLOSE DB CONNECTION */
 if (ologof(&lda))                                                                   { showError(&lda); } 
   
     }
-----------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------

Build and run the above program.

Note:Before running above project ..Create the people table in the oracle database.
create table people(name char(20),age int ,born int);

Check the database by select * from people.


























No comments:

Post a Comment

Cool Blue Outer Glow Pointer