Skip to content
Home » OrclDB » Pro*C - Connect to Oracle Database

Pro*C - Connect to Oracle Database

Here is an example of the Pro*C program to connect to the Oracle Database.

Pro*C Program Example - Connect to Oracle Database

The below Pro*C program will connect to the Oracle database and will execute the stored procedure YourDBProc.

#include <stdio.h>
#include <string.h>
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR username[20];
VARCHAR pwd[20];

EXEC SQL END DECLARE SECTION;
EXEC SQL INCLUDE SQLCA.H;
int sqlerror();
int main()
{
strcpy((char *)username.arr,"scott");
username.len = (short) strlen((char *)username.arr);
strcpy((char *)pwd.arr,"tiger");
pwd.len = (short) strlen((char *)pwd.arr);

EXEC SQL WHENEVER SQLERROR DO sqlerror();

EXEC SQL CONNECT :username IDENTIFIED BY :pwd;
EXEC SQL EXECUTE
BEGIN
YourDBProc;
END;
END-EXEC;

EXEC SQL COMMIT WORK RELEASE;
exit(0);
}
sqlerror()
{
EXEC SQL WHENEVER SQLERROR CONTINUE;
printf("\n% .70s \n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK WORK RELEASE;
exit(1);
}