📄 sqlbase.cpp
字号:
// SqlDB.cpp: implementation of the CSqlDB class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SQLBase.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// Prototypes for DB-Library error and message handling.
int err_handler(DBPROCESS*, int, int, int, char*, char*);
int msg_handler(DBPROCESS*, DBINT, int, int, char*);
extern CRITICAL_SECTION g_DBCritical;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSqlDB::CSqlDB()
{
}
CSqlDB::~CSqlDB()
{
}
/***************************************************************
函数描述: 连接登录数据库
函数性质: public
函数类型: void
输入参数: char *Server --- 服务器名
char *UserID --- 用户名
char *Password --- 密码
int iInitTime --- 连接超时值
输出参数: char *ErrMsg --- 错误提示信息
返回 值: 成功: ture 失败:false
作 者: 阿义
***************************************************************/
bool CSqlDB::OnDBInit(char *Server,
char *UserID,
char *Password,
int iInitTime,
char *pchDbName,
char *ErrMsg)
{
// check to see if communications layer was loaded (DOS ONLY)
m_chSQLVersion = dbinit();
if(m_chSQLVersion == (const char *)NULL)
{
// DOS TSR (DBNMPIPE.EXE) is not loaded, don't bother going any farther
sprintf(ErrMsg,"%s","初始化DB-Library失败,可能已经初始化了");
return false;
}
dbsettime(iInitTime); // set timeouts to iInitTime seconds
// set error/msg handlers for this program
//dbmsghandle((DBMSGHANDLE_PROC)msg_handler);
//dberrhandle((DBERRHANDLE_PROC)err_handler);
login = dblogin();
if (login == NULL)
{
sprintf(ErrMsg, "%s", "登陆sqlserver数据库失败");
return false;
}
//dbchange
DBSETLUSER(login,UserID); // set login id
DBSETLHOST(login,Server); // set host name for sp_who
DBSETLPWD(login,Password);
DBSETLVERSION(login, DBVER60);
// To use secure, or trusted, connection, uncomment the following line.
//DBSETLSECURE (login);
// open connection to requested server. Pass null server name for local
// connection, if name not entered.
dbproc = dbopen(login,(*Server) ? Server : (char *)NULL);
//DWORD dError = GetLastError();
//dberrhandle (login);
if(dbproc == (DBPROCESS *)NULL)
{
// no one answered, so couldn't connect or error occurred
sprintf(ErrMsg,"%s","打开数据库失败");
return false;
}
if (dbuse(dbproc,pchDbName)==FAIL)
{
sprintf(ErrMsg, "%s", "使用数据库失败!");
return false;
}
return true;
}
int err_handler(DBPROCESS *dbproc, int Severity, int dberr, int oserr, char *errstr, char *oserrstr)
{
char temp[300];
sprintf(temp,"DB-LIBRARY Error - Severity: %d, Error No: %d, OS Error No: %d",
Severity, dberr, oserr);
MessageBox(NULL,temp,"Info",MB_OK);
if(errstr != NULL)
{
sprintf(temp,"%s\n",errstr);
MessageBox(NULL,temp,"info",MB_OK);
}
if(oserrstr != NULL)
{
sprintf(temp,"%s",oserrstr);
MessageBox(NULL,temp,"info",MB_OK);
}
return INT_CANCEL;
}
int msg_handler(DBPROCESS *dbproc, DBINT Msg, int State, int Severity, char *Message)
{
char temp[300];
sprintf(temp,"Message No.: %ld, Msg. State: %d, Msg. Severity: %d",
Msg, State, Severity);
MessageBox(NULL, temp,"info",MB_OK);
if(Message != NULL)
{
sprintf(temp,"%s",Message);
MessageBox(NULL, temp,"info",MB_OK);
}
return (0);
}
RETCODE CSqlDB::OnOutputRow(DBPROCESS *dbproc, char *Output)
{
int x,cols,size,datasize,colwidth,coltype; // counters
char *datavals; // data buffer pointer
char *data; // column data pointer
char *ptr; // scratch pointer
colwidth = OnDetermineRowSize(dbproc,0);
ptr = datavals = (char *)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 = OnDetermineRowSize(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 == (char *)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,(unsigned char *)data,dbdatlen(dbproc,x),
SQLCHAR,(unsigned char*)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
memcpy(Output,datavals,strlen(datavals));
free(datavals); // free buffer
return SUCCEED; // done
}
RETCODE CSqlDB::OnPrintHeaders(DBPROCESS *dbproc)
{
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 = OnDetermineRowSize(dbproc,0); // get size of buffers
ptr = colnames =(char *)malloc(size+1); // get name buffer
hptr = header =(char *)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 = OnDetermineRowSize(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';
MessageBox(NULL,colnames,"Info",MB_OK); // print both strings
MessageBox(NULL,header,"Info",MB_OK);
free(colnames); // free both buffers
free(header);
return SUCCEED; // done
}
int CSqlDB::OnDetermineRowSize(DBPROCESS *dbproc, int col)
{
int iColNum = 0; //第几列
int iTotalCols = 0; // counters
int iLength = 0; // total length of column(row).
int iPrintLength = 0; // printable length
if (!col) // get number of columns
iTotalCols = dbnumcols(dbproc);
// count from 1 to numcols if col is 0, else x will = col only
for( iColNum =( (col) ? col : 1); iColNum<=((col) ? col : iTotalCols); iColNum++)
{
switch(dbcoltype(dbproc, iColNum)) // get column type, determine SQLCHAR
{ // converted length
case SQLNUMERIC:
case SQLDECIMAL:
{
DBCOL dbCol;
dbCol.SizeOfStruct = sizeof(DBCOL);
dbcolinfo(dbproc, CI_REGULAR, iColNum, 0, &dbCol);
iPrintLength = dbCol.Precision + 2;
}
break;
case SQLBIT: // The PR... values are found in the
iPrintLength = PRBIT; // SQLDB.H header file.
break;
case SQLINT1:
iPrintLength = PRINT1;
break;
case SQLINT2:
iPrintLength = PRINT2;
break;
case SQLINT4:
iPrintLength = PRINT4;
break;
case SQLFLT8:
iPrintLength = PRFLT8;
break;
case SQLDATETIME:
iPrintLength = PRDATETIME;
break;
case SQLMONEY:
iPrintLength = PRMONEY;
break;
case SQLVARBINARY: // VARBINARY IMAGE, and BINARY
case SQLBINARY: // convert to 2 times length
case SQLIMAGE:
iPrintLength = dbcollen(dbproc, iColNum) * 2;
break;
default :
iPrintLength = dbcollen(dbproc, iColNum); // 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 (iPrintLength < namelength) // longer of two.
// length += namelength + 1; // add one for space between
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -