VS2008 使用 occi 连接 Oracle 服务器- 不用安装客户端

到Oracle官网上下载

instantclient-basic-win32-11.1.0.6.0、

instantclient-sdk-win32-11.1.0.6.0

occivc9win32-111060-13218

需要动态库文件

 头文件  instantclient-sdk-win32-11.1.0.6.0\instantclient_11_1\sdk路径下的include文件夹
 库文件 oraocci11.lib  

 DLL文件  oraocci11.dll     oci.dll    oraociei11.dll 
#include 
#include 
using namespace std;
using namespace oracle::occi;

#pragma comment(lib,"oraocci11d.lib")

int main()
{
    Environment* env = NULL;
    Connection* conn = NULL;
    Statement *stmt = NULL;
    try
    {
        char strSql[1000];
        ResultSet *rs = NULL; 


        env =Environment::createEnvironment((Environment::Mode)(Environment::OBJECT|Environment::THREADED_UNMUTEXED));
        conn =env->createConnection("ugprs","Sys123456","//192.168.0.148:1521/gprs");
        stmt = conn->createStatement();

        sprintf(strSql,"%s","insert into ghy_test(num,name,age)values(:1,:2,:3)");
        stmt->setSQL(strSql);

        stmt->setNumber(1,1);
        stmt->setString(2,"sky");
        stmt->setNumber(3,12);
        stmt->executeUpdate();
        conn->commit();


        // 本人的机器上 要加双引号   select "name" from "ghy_test" 才可以
        stmt = conn->createStatement("select name from ghy_test"); 

        rs = stmt->executeQuery(); 


        while(rs->next()) 
        { 

           // 起始 是 1 不是 0  ,也是 getNumber getInt  函数 ,getString 会把数据转换成 string

           cout << rs->getString(1) << endl; 
        } 

        env->terminateConnection(conn);
        Environment::terminateEnvironment(env);
    }
    catch(SQLException& e)
    {
        cout << e.what()<if (conn != NULL)
            env->terminateConnection(conn);
        if (env != NULL)
            Environment::terminateEnvironment(env);
        return -1;
    }
    return 0;
}

你可能感兴趣的:(数据库)