?? db_function.c
字號:
/* DB_libry 封裝函數,連結 sybase */
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/************************************************
* Description : Initialize DB-Library
* Function : db_init()
* Input :
* Ouput : int 由返回值判斷初試化狀態
************************************************/
int DB_init(void)
{
/* Initialize DB-Library. */
if (dbinit() == FAIL) return(-1);
//dbsetversion(DBVERSION_100);
return(1);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**************************************************
* Description: Connect database server
* Function : db_connect()
* Input : char * Dbname 數據庫名稱
* char * YsrName 用戶名
* char * UsrPwd 密碼
* Ouput : DBPROCESS * dbproc 獲得的process
***************************************************/
DBPROCESS* DB_connect (char *DbName,char *UsrName,char *UsrPwd)
{
DBPROCESS *dbproc;
LOGINREC *login;
login = dblogin();
DBSETLUSER(login, UsrName);
DBSETLPWD(login, UsrPwd);
//BCP_SETL(login, TRUE) ; //?????
dbproc = dbopen(login, NULL);
dbloginfree(login); //一定要釋放dbloginfree
if(dbproc!=NULL) dbuse(dbproc, DbName);
else return(NULL);
return(dbproc);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/******************************************************
* Description: Disconnect Database Server
* Function : DB_disconnect()
* Input :
* Output :
*******************************************************/
void DB_disconnect(DBPROCESS* dbproc)
{
dbclose(dbproc);
dbexit();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**********************************************************************
* Description : Send some Sql to the server with dbsqlexec
* Function : DB_sqlexec()
* Input : DBPROCESS * dbporc
* int Sql_type Sql語句的類型 1 為select 0為其他
* char * SqlStr Sql語句
* int units 預計取得的一條記錄的單元個數(不超過20)
* int records 預計取得的記錄數
* void * temp_space 存儲空間的地址
* int record_len 單元的最大長度(不超過2048)
* Output : int
***********************************************************************/
int DB_sqlexec(DBPROCESS* dbproc,char * SqlStr,int Sql_type,int units,int records,int record_len,void* temp_space)
{
//DBPROCESS *dbproc; /* Our connection with SQL Server. */
RETCODE result_code;
DBCHAR text[20][2048];
int i;
int j;
/* 判斷 process 是否為空 */
if (dbproc ==(DBPROCESS*)NULL)
{
printf("DBPROCESS NULL SqlStr:%s\n",SqlStr);
return(-1);
}
/* 執行Sql語句 */
dbcmd(dbproc,SqlStr);
dbsqlexec(dbproc);
/* 判斷返回值,是否成功 */
result_code = dbresults(dbproc);
i = 0;
j = 0;
if(result_code == SUCCEED)
{
if(Sql_type == 1)
/* select 型SQL語句 */
{
/* BIND 足夠多的單元數 */
while( j < units )
{
dbbind(dbproc, j+1, NTBSTRINGBIND, (DBINT)0,text[j]);
j++;
printf("bind %d\n",j);
}
/* 從PROCESS上取得數據 */
while (dbnextrow(dbproc) != NO_MORE_ROWS)
{
j = 0;
if ( i < records )//判斷所取數據有否超過預留存儲空間
{
while(j < units)
{
/* copy至對應存儲空間 */
memcpy(temp_space+i*units*record_len+j*record_len,text[j],record_len);
j++;
}
}
i++;
}
}
/*INSERT,UPDATE型SQL語句*/
else
{
return(0);
}
return(i);
}
else
{
printf("sql error sql:%s\n",SqlStr);
return(-1);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**************************************************************************
*Description : Get record
*Function : Get_record()
*Intput : void * start_data
* char * record
* int num 第幾條記錄,從0開始記數
* int r_len 記錄的長度(=在DB_sqlexec()中的 record_len)
*Output : int 0為成功,-1為失敗
***************************************************************************/
int Get_record(void *start_data,char * record,int num,int r_len)
{
memcpy(record,start_data+num*r_len,r_len);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -