我在Linux + eclipse下编程访问远端oracle数据库,我的机子上已装了oracle 的instant client ,采用oci库,其中库定义函数原形:OCIServerAttach ( OCIServer    *srvhp,  OCIError     *errhp, const OraText   *dblink,    sb4      dblink_len,      ub4          mode);我想第三个参数就是远程oracle数据库的IP地址之类,我调用如下:
char Tns[50];
strcpy(Tns,“172.66.1.2");  //  Ip地址
......errorNo = OCIServerAttach(hSvr, hDBErr, (text*)Tns, strlen(Tns), 0);"
结果报错:
ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA我以为是没指定监听端口,修改:
strcpy(Tns,“172.66.1.2:1521");  //  前面为Ip地址,后面为监听端口
errorNo = OCIServerAttach(hSvr, hDBErr, (text*)Tns, strlen(Tns), 0);"
结果报错:Cannot insert breakpoint -41.
Error accessing memory address 0xc1d9a6b2: \350\276\223\345\205\245/\350\276\223\345\207\272\351\224\231\350\257\257.

解决方案 »

  1.   

    我也想知道,谁会啊!
    对了,我用的是OCCI,我想问下eclipse的编译环境怎么配置的啊,
    我setting了include和libarary但是不行啊。还请你指教啊。谢谢!!!
      

  2.   

    1) Initialize the OCI using OCIInitialize2) Initialize the environment using OCIEnvInit3) We create four handles for error, server and service context and a user session.4) Then we do a server attach using OCIServerAttach by passing the allocated server handle. 5) We then set the server attribute (OCI_ATTR_SERVER) of the service context using OCIAttrSet.6) We then execute OCISessionBegin and pass the mode as OCI_CRED_EXT.
    The above sequence works fine if ORACLE_SID is set in the environment and if we pass an empty string for the parameter "dblink" in OCIServerAttach.
      

  3.   

    这有个例子你看看。
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <oci.h>int VerifyConnection(text *database, text *username, text *password);int CheckError(OCIError *errhp, sword status, text *errbuf, size_t errbufsize, sb4 *errcode);
    void cleanup(OCIEnv *envhp, OCIError *errhp, OCISession *authp, OCIServer *srvhp, OCISvcCtx *svchp);int VerifyConnection(text *database, text *username, text *password)
    {
    sword status = 0;
    text errbuff512;
    size_t errbuffsize = sizeof(errbuff);
    sb4 errorcode = 0;
    sb4 *errcode = &errorcode;OCIEnv *envhp = (OCIEnv *) 0;
    OCIError *errhp = (OCIError *) 0;
    OCISession *authp = (OCISession *) 0;
    OCIServer *srvhp = (OCIServer *) 0;
    OCISvcCtx *svchp = (OCISvcCtx *) 0;fprintf(stderr, "\n=== VerifyConnection function ===\n");printf("Initialize...\n");
    status = OCIInitialize((ub4) OCI_DEFAULT, (dvoid *)0,
    (dvoid * (*)(dvoid *, size_t)) 0,
    (dvoid * (*)(dvoid *, dvoid *, size_t))0,
    (dvoid (*)(dvoid *, dvoid *)) 0 );
    if (CheckError(errhp, status, errbuff, errbuffsize, errcode)) {
    cleanup(envhp, errhp, authp, srvhp, svchp);
    return 0;
    }printf("EnvInit...\n");
    status = OCIEnvInit( (OCIEnv **) &envhp, OCI_DEFAULT, (size_t) 0,
    (dvoid **) 0 );
    if (CheckError(errhp, status, errbuff, errbuffsize, errcode)) {
    cleanup(envhp, errhp, authp, srvhp, svchp);
    return 0;
    }printf("HandleAlloc Error...\n");
    status = OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR,
    (size_t) 0, (dvoid **) 0);
    if (CheckError(errhp, status, errbuff, errbuffsize, errcode)) {
    cleanup(envhp, errhp, authp, srvhp, svchp);
    return 0;
    }/* server contexts */
    printf("HandleAlloc Server...\n");
    status = OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &srvhp, OCI_HTYPE_SERVER,
    (size_t) 0, (dvoid **) 0);
    if (CheckError(errhp, status, errbuff, errbuffsize, errcode)) {
    cleanup(envhp, errhp, authp, srvhp, svchp);
    return 0;
    }printf("HandleAlloc Service Context...\n");
    status = OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &svchp, OCI_HTYPE_SVCCTX,
    (size_t) 0, (dvoid **) 0);
    if (CheckError(errhp, status, errbuff, errbuffsize, errcode)) {
    cleanup(envhp, errhp, authp, srvhp, svchp);
    return 0;
    }printf("ServerAttach...\n");
    status = OCIServerAttach( srvhp, errhp, (text *)database, strlen((char *)database), 0);
    if (CheckError(errhp, status, errbuff, errbuffsize, errcode)) {
    cleanup(envhp, errhp, authp, srvhp, svchp);
    return 0;
    }/* set attribute server context in the service context */
    printf("AttrSet ServiceContext Server...\n");
    status = OCIAttrSet( (dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp,
    (ub4) 0, OCI_ATTR_SERVER, (OCIError *) errhp);
    if (CheckError(errhp, status, errbuff, errbuffsize, errcode)) {
    cleanup(envhp, errhp, authp, srvhp, svchp);
    return 0;
    }printf("HandleAlloc Session...\n");
    status = OCIHandleAlloc((dvoid *) envhp, (dvoid **)&authp,
    (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0);
    if (CheckError(errhp, status, errbuff, errbuffsize, errcode)) {
    cleanup(envhp, errhp, authp, srvhp, svchp);
    return 0;
    }printf("AttrSet Session Username...\n");
    status = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
    (dvoid *) username, (ub4) strlen((char *)username),
    (ub4) OCI_ATTR_USERNAME, errhp);
    if (CheckError(errhp, status, errbuff, errbuffsize, errcode)) {
    cleanup(envhp, errhp, authp, srvhp, svchp);
    return 0;
    }
    printf("AttrSet Session Password...\n");
    status = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
    (dvoid *) password, (ub4) strlen((char *)password),
    (ub4) OCI_ATTR_PASSWORD, errhp);
    if (CheckError(errhp, status, errbuff, errbuffsize, errcode)) {
    cleanup(envhp, errhp, authp, srvhp, svchp);
    return 0;
    }printf("SessionBegin...\n");
    status = OCISessionBegin ( svchp, errhp, authp, OCI_CRED_RDBMS,
    (ub4) OCI_DEFAULT);
    if (CheckError(errhp, status, errbuff, errbuffsize, errcode)) {
    cleanup(envhp, errhp, authp, srvhp, svchp);
    return 0;
    }printf("AttrSet ServiceContext Session...\n");
    status = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
    (dvoid *) authp, (ub4) 0,
    (ub4) OCI_ATTR_SESSION, errhp);
    if (CheckError(errhp, status, errbuff, errbuffsize, errcode)) {
    cleanup(envhp, errhp, authp, srvhp, svchp);
    return 0;
    }/* connected successfully
    --------------------------------------------------------------------------------
    */printf("\nConnected Successfully.\n");cleanup(envhp, errhp, authp, srvhp, svchp);envhp = (OCIEnv *) 0;
    errhp = (OCIError *) 0;
    authp = (OCISession *) 0;
    srvhp = (OCIServer *) 0;
    svchp = (OCISvcCtx *) 0;return 1; /* return success */
    }int CheckError(OCIError *errhp, sword status, text *errbuf, size_t errbufsize, sb4 *errcode)
    {
    switch (status)
    {
    case OCI_SUCCESS:
    /*printf("\tOCI_SUCCESS\n");*/
    return 0;
    case OCI_SUCCESS_WITH_INFO:
    /* OCI_SUCCESSS_WITH_INFO status is considered a Warning instead of an Error */
    printf("\tError - OCI_SUCCESS_WITH_INFO\n");
    if (errhp) 
    {
    OCIErrorGet((dvoid *)errhp, (ub4) 1, (text *) NULL, &errcode,
    errbuf, (ub4) errbufsize, OCI_HTYPE_ERROR);
    printf("\t\tError - %.*s\n", errbufsize, errbuf);
    printf("\t\tError Code: %d\n", errcode);
    }
    break;
    case OCI_NEED_DATA:
    printf("\tError - OCI_NEED_DATA\n");
    break;
    case OCI_NO_DATA:
    printf("\tError - OCI_NODATA\n");
    break;
    case OCI_ERROR:
    printf("\tError - OCI_ERROR.\n");
    if (errhp) 
    {
    OCIErrorGet((dvoid *)errhp, (ub4) 1, (text *) NULL, &errcode,
    errbuf, (ub4) errbufsize, OCI_HTYPE_ERROR);
    printf("\t\tError - %.*s\n", errbufsize, errbuf);
    printf("\t\tError Code: %d\n", errcode);
    }
    break;
    case OCI_INVALID_HANDLE:
    printf("\tError - OCI_INVALID_HANDLE\n");
    break;
    case OCI_STILL_EXECUTING:
    printf("\tError - OCI_STILL_EXECUTE\n");
    break;
    case OCI_CONTINUE:
    printf("\tError - OCI_CONTINUE\n");
    break;
    default:
    printf("\tError - default\n");
    break;
    }return 1;
    }void cleanup(OCIEnv *envhp, OCIError *errhp, OCISession *authp, OCIServer *srvhp, OCISvcCtx *svchp)
    {
    sword status = 0;printf("\nCleanup.\n");if(svchp && errhp && authp)
    status = OCISessionEnd(svchp, errhp, authp, (ub4) 0);
    if(srvhp && errhp)
    status = OCIServerDetach(srvhp, errhp, (ub4) OCI_DEFAULT);if (authp)
    status = OCIHandleFree((dvoid *) authp, (ub4) OCI_HTYPE_SESSION);if (srvhp)
    status = OCIHandleFree((dvoid *) srvhp, (ub4) OCI_HTYPE_SERVER);if (svchp)
    status = OCIHandleFree((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX);if (errhp)
    status = OCIHandleFree((dvoid *) errhp, (ub4) OCI_HTYPE_ERROR);if (envhp)
    status = OCIHandleFree((dvoid *) envhp, (ub4) OCI_HTYPE_ENV);envhp = (OCIEnv *) 0;
    errhp = (OCIError *) 0;
    authp = (OCISession *) 0;
    srvhp = (OCIServer *) 0;
    svchp = (OCISvcCtx *) 0;
    }int main(int argc, char *argv[])
    {
    int result = 0;char *database = "TESTDB";
    char *username = "SCOTT";
    char *oldpassword = "TIGER";
    char *newpassword = "FROG";result = VerifyConnection((text *)database, (text *)username, (text *)oldpassword);
    if (result == 1)
    printf("*** Connection Good ***\n");
    else 
    printf("*** Connection Bad ***\n");return 0;
    }
      

  4.   

    在eclipse下单步执行sword errorNo = OCIServerAttach(hSvr, hDBErr, (text*)Tns, strlen(Tns), 0);
    一句报错:Cannot insert breakpoint -41.
    Error accessing memory address 0xc1d9a6b2: \350\276\223\345\205\245/\350\276\223\345\207\272\351\224\231\350\257\257.
    恐怕不是真正报错,光标设在OCIServerAttach(hSvr, hDBErr, (text*)Tns, strlen(Tns), 0)以下的语句,点菜单“run to line”,以不停顿的方式
    掠过这一句,无报错!就这么所谓错误折腾了两天,把我搞惨了!!!煎熬呀!!!!谢谢楼上兄弟,你的问题我已短信给你!不知有用吗?
      

  5.   

    详细解释我11.14的回复:解决问题了:原来是个伪问题:因为只是个警告:Warning: Cannot insert breakpoint -41. Error accessing memory address 0xc1d9a6b2: \350\276\223\345\205\245/\350\276\223\345\207\272\351\224\231\350\257\257.所以不以单步执行 errorNo = OCIServerAttach(hSvr, hDBErr, (text*)Tns, strlen(Tns), 0);" ,就不会出警告!即使单步执行此句,不理此警告,继续单步下去,也能成功执行完此句!折腾了两天,很煎熬!才发现原来是个伪问题!
    前车之鉴!
    另外我发现在linux下装了访问oracle的客户端instant client 后,似乎真的不需要tnsnames.ora 文件,oracle官方instant client介绍也是这么说的:见连接:http://download.oracle.com/docs/cd/B12037_01/appdev.101/b10779/oci01int.htm#423364中的此句:All Oracle net naming methods that do not require use of ORACLE_HOME or TNS_ADMIN (to locate configuration files such as tnsnames.ora or sqlnet.ora) work in the Instant Client mode,不知我的理解对否?多谢各位热心人!