⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_conn.c

📁 The zip file contais a sample program to check the sybase database connectivity. I ve attached the m
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctpublic.h>
#include <bkpublic.h>
#include "example.h"
#include "exutils.h"
#include <Windows.h>

/*****************************************************************************
** 
** defines and globals used.
** 
*****************************************************************************/

#define SEC 15     // 10 Seconds waiting so that we could use QuickEditor
#define MAXSIZE 20

CS_RETCODE CS_PUBLIC myex_ctx_cleanup(CS_CONTEXT *context, CS_RETCODE status);
CS_RETCODE CS_PUBLIC myex_con_cleanup(CS_CONNECTION *connection, CS_RETCODE status);
CS_RETCODE CS_PUBLIC myex_init(CS_CONTEXT **context);

/* MY DB DETAILS */
CS_CHAR *Ex_appname  = "db_connection";
CS_CHAR *Ex_dbname   = "PT849";
CS_CHAR *Ex_server   = "PTIBMASE";
CS_CHAR *Ex_username = "people";
CS_CHAR Ex_password[MAXSIZE] = "";

#define SLEEP(x)  do { printf("\nSleeping for %d secs...\n",x); \
                       Sleep(1000 * x); }while(0);

/*
** Prototypes for routines in sample code
*/ 
CS_STATIC CS_RETCODE ConnectToDB PROTOTYPE((
    CS_CONTEXT *context,        
    CS_CONNECTION **connection
    ));

int
main(int argc, char *argv[])
{
    CS_CONTEXT    *context = NULL;
    CS_CONNECTION    *connection = NULL;
    CS_RETCODE    retcode;
    
    EX_SCREEN_INIT();

    fprintf(stdout, "Simple connection example \n");
    fflush(stdout);

    /* 
    ** Allocate a context structure and initialize Client-Library 
    */
    retcode = myex_init(&context);
    if (retcode != CS_SUCCEED)
    {
        fprintf(stderr, "myex_init failed \n");
    }

    /* 
    ** Allocate a connection structure, set its properties, and  
    ** establish a connection. Since the blk example needs to define
    ** additional properties before login, it uses its own connection
    ** routine.
    */
    if ( argc > 3 ) {
        int server_len = strlen(argv[1]);
        int dbname_len = strlen(argv[2]);
        int user_len = strlen(argv[3]);
        Ex_server = (char *) malloc(server_len+1);
        Ex_dbname = (char *) malloc(dbname_len+1);
        Ex_username = (char *) malloc(user_len+1);

        strncpy(Ex_server, argv[1], server_len);
        strncpy(Ex_dbname, argv[2], dbname_len);
        strncpy(Ex_username, argv[3], user_len);
        Ex_server[server_len]='\0';
        Ex_dbname[dbname_len]='\0';
        Ex_username[user_len]='\0';
        printf("Connecting to %s %s using %s \n", Ex_server, Ex_dbname, Ex_username);
    }

    retcode = ConnectToDB(context, &connection);

    fprintf(stdout, "Will be doing a cleanup in 10 secs\n");
        SLEEP(SEC); 
    fprintf(stdout, "Starting cleanup \n");
    /*
    ** Deallocate the allocated structures, close the connection,
    ** and exit Client-Library.
    */
    if (connection != NULL)
    {
        retcode = myex_con_cleanup(connection, retcode);
    }
    
    if (context != NULL)
    {
        retcode = myex_ctx_cleanup(context, retcode);
    }

        SLEEP(SEC + 10); 

    return (retcode == CS_SUCCEED) ? EX_EXIT_SUCCEED : EX_EXIT_FAIL;
}

/*
** ConnectToDB()
**
*/

CS_STATIC CS_RETCODE
ConnectToDB(CS_CONTEXT *context, CS_CONNECTION **connection)
{
    CS_INT        len;
    CS_RETCODE    retcode;
    CS_BOOL       abool;

    /* 
    ** Allocate a connection structure. 
    */
    retcode = ct_con_alloc(context, connection);
    if (retcode != CS_SUCCEED)
    {
        fprintf(stderr, "ct_con_alloc failed");
        return retcode;
    }

    /*    
    ** username is defined.
    */
    if (retcode == CS_SUCCEED && Ex_username != NULL)
    {
        if ((retcode = ct_con_props(*connection, CS_SET, CS_USERNAME, 
                Ex_username, CS_NULLTERM, NULL)) != CS_SUCCEED)
        {
            fprintf(stderr,"ct_con_props(username) failed");
            return(retcode);
        }
    }

    /*
    ** Enable password encryption
    */
    if (retcode == CS_SUCCEED)
    {
        abool = CS_TRUE;
        retcode = ct_con_props(*connection, CS_SET, CS_SEC_ENCRYPTION,
                                       (CS_VOID *)&abool, CS_UNUSED, (CS_INT*)NULL);
        if (retcode != CS_SUCCEED)
        {
                   fprintf(stderr,"ct_con_props(CS_SEC_ENCRYPTION) failed");
            return(retcode);
        }
    }

    /*    
    ** Prompt user to enter the password.
    */
    memset(Ex_password, 0, MAXSIZE);
    fprintf(stdout,"Enter the password :");
    //fgets(Ex_password, MAXSIZE, stdin);
    gets(Ex_password);
    fflush(stdin);
    SLEEP(SEC);

    if (retcode == CS_SUCCEED && Ex_password != NULL)
    {
        if ((retcode = ct_con_props(*connection, CS_SET, CS_PASSWORD, 
                Ex_password, CS_NULLTERM, NULL)) != CS_SUCCEED)
        {
            fprintf(stderr,"ct_con_props(passwd) failed\n");
            return(retcode);
        }
        fprintf(stdout,"ct_con_props(passwd) successful\n");
    }

    memset(Ex_password, 0, MAXSIZE);

    SLEEP(SEC); 

    /*    
    ** Set the CS_APPNAME property.
    */
    if (retcode == CS_SUCCEED && Ex_appname != NULL)
    {
        if ((retcode = ct_con_props(*connection, CS_SET, CS_APPNAME, 
                Ex_appname, CS_NULLTERM, NULL)) != CS_SUCCEED)
        {
            fprintf(stderr,"ct_con_props(appname) failed");
            return(retcode);
        }
        fprintf(stderr,"ct_con_props(appname) successful\n");
    }


    /*    
    ** Open a Server connection.
    */
    if (retcode == CS_SUCCEED)
    {
        len = (Ex_server == NULL) ? 0 : CS_NULLTERM;
        retcode = ct_connect(*connection, Ex_server, len);
        if (retcode != CS_SUCCEED)
        {
            fprintf(stderr,"ERROR : ct_connect failed\n");
            ct_con_drop(*connection);
            *connection = NULL;
            return(retcode);
        }
        fprintf(stderr,"ct_connect successful\n");
    }

    if (retcode != CS_SUCCEED)
    {
        ct_con_drop(*connection);
        *connection = NULL;
    }
    else
        fprintf(stdout, "Connection successfull\n");

    return retcode;
}


CS_RETCODE CS_PUBLIC
myex_ctx_cleanup(CS_CONTEXT *context, CS_RETCODE status)
{
    CS_RETCODE    retcode;
    CS_INT        exit_option;

    exit_option = (status != CS_SUCCEED) ? CS_FORCE_EXIT : CS_UNUSED;
    retcode = ct_exit(context, exit_option);
    if (retcode != CS_SUCCEED)
    {
        fprintf(stderr,"myex_ctx_cleanup: ct_exit() failed");
        return retcode;
    }
    retcode = cs_ctx_drop(context);
    if (retcode != CS_SUCCEED)
    {
        fprintf(stderr,"myex_ctx_cleanup: cs_ctx_drop() failed");
        return retcode;
    }
    return retcode;
}


CS_RETCODE CS_PUBLIC
myex_con_cleanup(CS_CONNECTION *connection, CS_RETCODE status)
{
    CS_RETCODE    retcode;
    CS_INT        close_option;

    close_option = (status != CS_SUCCEED) ? CS_FORCE_CLOSE : CS_UNUSED;
    retcode = ct_close(connection, close_option);
    if (retcode != CS_SUCCEED)
    {
        fprintf(stderr, "myex_con_cleanup: ct_close() failed");
        return retcode;
    }
    retcode = ct_con_drop(connection);
    if (retcode != CS_SUCCEED)
    {
        fprintf(stderr, "myex_con_cleanup: ct_con_drop() failed");
        return retcode;
    }

    return retcode;
}


CS_RETCODE CS_PUBLIC
myex_init(CS_CONTEXT **context)
{
    CS_RETCODE    retcode;
    CS_INT        netio_type = CS_SYNC_IO;

    /*
    ** Get a context handle to use.
    */
    retcode = cs_ctx_alloc(EX_CTLIB_VERSION, context);
    if (retcode != CS_SUCCEED)
    {
        fprintf(stderr,"myex_init: cs_ctx_alloc() failed");
        return retcode;
    }

    /*
    ** Initialize Open Client.
    */
    retcode = ct_init(*context, EX_CTLIB_VERSION);
    if (retcode != CS_SUCCEED)
    {
        fprintf(stderr,"myex_init: ct_init() failed");
        cs_ctx_drop(*context);
        *context = NULL;
        return retcode;
    }

#ifdef EX_API_DEBUG_CTX
    /*
    ** ct_debug stuff. Enable this function right before any call to
    ** OC-Lib that is returning failure.
    */
    retcode = ct_debug(*context, NULL, CS_SET_FLAG, CS_DBG_API_STATES,
                NULL, CS_UNUSED);
    if (retcode != CS_SUCCEED)
    {
        fprintf(stderr, "myex_init: ct_debug() failed");
    }
#endif

    /*
    ** Install client and server message handlers.
    */
    if (retcode == CS_SUCCEED)
    {
        retcode = ct_callback(*context, NULL, CS_SET, CS_CLIENTMSG_CB,
                    (CS_VOID *)ex_clientmsg_cb);
        if (retcode != CS_SUCCEED)
        {
            fprintf(stderr,"myex_init: ct_callback(clientmsg) failed");
        }
    }

    if (retcode == CS_SUCCEED)
    {
        retcode = ct_callback(*context, NULL, CS_SET, CS_SERVERMSG_CB,
                (CS_VOID *)ex_servermsg_cb);
        if (retcode != CS_SUCCEED)
        {
            fprintf(stderr,"myex_init: ct_callback(servermsg) failed");
        }
    }

    /* 
    ** This is an synchronous example so set the input/output type
    ** to synchronous (This is the default setting, but show an
    ** example anyway).
    */
    if (retcode == CS_SUCCEED)
    {
        retcode = ct_config(*context, CS_SET, CS_NETIO, &netio_type, 
                        CS_UNUSED, NULL);
        if (retcode != CS_SUCCEED)
        {
            fprintf(stderr,"myex_init: ct_config(netio) failed");
        }
    }

    if (retcode != CS_SUCCEED)
    {
        ct_exit(*context, CS_FORCE_EXIT);
        cs_ctx_drop(*context);
        *context = NULL;
    }

    return retcode;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -