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

📄 sqlexamp.c

📁 本代码展示了使用DB_LIB库对SQL SERVER的连接和各种操作的实例。
💻 C
📖 第 1 页 / 共 2 页
字号:
                prlength = Col.Precision + 2;
                }
                break;

            case SQLBIT:            // The PR... values are found in the
                prlength = PRBIT;   // SQLDB.H header file.
                break;
            case SQLINT1:
                prlength = PRINT1;
                break;
            case SQLINT2:
                prlength = PRINT2;
                break;
            case SQLINT4:
                prlength = PRINT4;
                break;
            case SQLFLT8:
                prlength = PRFLT8;
                break;
            case SQLDATETIME:
                prlength = PRDATETIME;
                break;
            case SQLMONEY:
                prlength = PRMONEY;
                break;
            case SQLVARBINARY:      // VARBINARY IMAGE, and BINARY
            case SQLBINARY:         // convert to 2 times length
            case SQLIMAGE:
                prlength = dbcollen(dbproc,x)*2;
                break;
            default :
                prlength = dbcollen(dbproc,x);  // other types are maximum of
                break;                          // actual column length
            }

        name = (char*) dbcolname(dbproc, x);    // names may be longer than
        namelength = (name) ? strlen(name) : 0; // column so use name len if

        if (prlength < namelength)      // longer of two.
            length += namelength + 1;   // add one for space between
        else                            // columns
            length += prlength + 1;
        }

    return length;              // return the length of the field
    }

/*
** RETCODE PrintHeaders(DBPROCESS *)
**
** This function builds the string that contains the names of each column,
** and a string containing '=' as a separator line.  It does this by finding
** the print size of each column, allocating a buffer to hold all column names
** plus one space between each column name, then copying that name into the
** appropriate location into the buffer.  Finally the two lines are
** printed.
*/
RETCODE PrintHeaders
    (
    DBPROCESS *dbproc       // The SQL Server connection structure pointer
    )
    {
    int x,cols,size;        // counters
    char *header;           // pointer for separator buffer
    char *colnames;         // pointer for column name buffer
    char *colname;          // scratch pointers
    char *ptr,*hptr;

    size = DetermineRowSize(dbproc,0);  // get size of buffers
    ptr = colnames = malloc(size+1);    // get name buffer
    hptr = header = malloc(size+1);     // get separator buf
    memset (header,' ',size);           // set buffers to all spaces
    memset (colnames,' ',size);
    
    cols = dbnumcols(dbproc);           // get number of columns
    for(x = 1; x <= cols; x++)          // loop on all columns
        {
        size = DetermineRowSize(dbproc,x);      // get size of this column
        colname = (char *)dbcolname(dbproc,x);  // get column name
        strncpy(ptr,colname,strlen(colname));   // copy name
        memset(hptr,'=',size-1);                // set ='s in separator line
        hptr+=size;                             // move to next position
        ptr+=size;                              // move to next position
        }

    *ptr = '\0';                // null term both strings
    *hptr = '\0';
    printf("%s\n",colnames);    // print both strings
    printf("%s\n",header);
    free(colnames);             // free both buffers
    free(header);

    return SUCCEED;             // done
    }

/*
** RETCODE PrintRow(DBPROCESS *)
**
** This function prints out one row.  dbnextrow() must be called to fetch the
** row to print.  This routine could be used to print the current row as
** many times as wanted, as the current row data is always available until
** dbnextrow() is called to fetch the next row.  This routine works like
** PrintHeaders above, but each column's data is obtained instead of a row
** name, and converted to a string.  It is then set into the buffer.
*/
RETCODE PrintRow
    (
    DBPROCESS *dbproc       // SQL Server connection structure
    )
    {
    int x,cols,size,datasize,colwidth,coltype;  // counters
    char *datavals;         // data buffer pointer
    char *data;             // column data pointer
    char *ptr;              // scratch pointer
    
    colwidth = DetermineRowSize(dbproc,0);
    ptr = datavals = malloc(colwidth+1);    // get buffer
    
    cols = dbnumcols(dbproc);               // get number of columns
    for(x=1;x<=cols;x++)                    // do all columns
        {
        coltype = dbcoltype(dbproc,x);
        size = DetermineRowSize(dbproc,x);  // determine size of this column
        memset(ptr,' ',size);               // set it to spaces
        data = (char *)dbdata(dbproc,x);    // get pointer to column's data
        if(data == (BYTE *)NULL)            // if NULL, use "NULL"
            {
            strncpy(ptr,"NULL",4);          // set NULL into buffer
            ptr += size;                    // point past this column in output buf
            }
        else                                // else have data, so convert to char
            {
            datasize = dbconvert(dbproc,coltype,data,dbdatlen(dbproc,x),
                SQLCHAR,ptr,(DBINT)size-1);
            if (datasize < size && (coltype == SQLNUMERIC || 
                coltype == SQLDECIMAL || coltype == SQLINT1 || 
                coltype == SQLINT2 || coltype == SQLINT4 || 
                coltype == SQLFLT8 || coltype == SQLFLT4))
                {
                memmove(ptr+size-1-datasize,ptr,datasize);
                memset(ptr,' ',size-1-datasize);
                }

            ptr += size;
            }
        }

    *ptr = '\0';                // null term string
    printf("%s\n",datavals);    // print row
    free(datavals);             // free buffer

    return SUCCEED;             // done
    }

/*
** msg_handler(char *buffer, long len);
**
** This routine is a local isql message handler call back function that
** is invoked whenever the SQL Server is sending a message back to
** the program.
**
*/
int msg_handler
    (
    DBPROCESS *dbproc,      // SQL Server connection structure
    DBINT Msg,              // SQL Server message number
    int State,              // State of the message
    int Severity,           // Severity of the message
    char *Message           // The message itself (read only)
    )
    {
    printf("Message No.: %ld, Msg. State: %d, Msg. Severity: %d\n",
        Msg, State, Severity);

    if(Message != NULL)
        printf("%s\n",Message);

    return (0);
    }

/*
** err_handler(char *buffer, long len);
**
** This routine is a local error handler called by dblib if an internal
** error occurs, also to notify when a server message has been sent, which is
** obtained through the above message handler.
**
*/
int err_handler
    (
    DBPROCESS *dbproc,  // SQL Server connection structure
    int Severity,       // Severity of Dblib error
    int dberr,          // dblib error, all dblib errors start at 10000
    int oserr,          // OS error from, C runtime
    char *errstr,       // dblib error string
    char *oserrstr      // OS error string (if any)
    )
    {

    printf("DB-LIBRARY Error - Severity: %d, Error No: %d, OS Error No: %d\n",
        Severity, dberr, oserr);

    if(errstr != NULL)
        printf("%s\n",errstr);
    if(oserrstr != NULL)
        printf("%s\n",oserrstr);

    return INT_CANCEL;
    }

/*****************************************************************************/
/*======================== E N D - O F - F I L E ============================*/
/*****************************************************************************/

⌨️ 快捷键说明

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