在BCC 5.5或者Dev C++ 4.9.x下编译过odbc程序的高手来看看,有问题请教
Laone 2003-07-17 11:09:07 到bcc 5.5和dev c ++ 4.9.x已经带了odbc库了,可是编译总成问题,在vc6下编译通过的c程序,在bcc和dev下就不行了,总是说
[Linker error] undefined reference to `SQLAllocHandle@12'
[Linker error] undefined reference to `SQLFreeHandle@8'
[Linker error] undefined reference to `SQLSetEnvAttr@16'
[Linker error] undefined reference to `SQLFreeHandle@8'
[Linker error] undefined reference to `SQLConnect@28'
程序例子:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <sql.h>
#include <sqlext.h>
#include <time.h>
#include <odbcinst.h>
void GetConfig(char typed[],char target[]);
HINSTANCE ghInst = 0;
HWND ghWnd = 0;
char t_buf[100000];
SQLHDBC hdbc; // hdbc
SQLRETURN nResult; // Result code
SQLHENV henv; // Environment Handle
BOOL Connectbl; //判断是否连接了数据库
//
//
/****************************************************************
* Function: ConnectDatabase()
* puerpose:连接数据库
* result:1:correct;-1:error;
*****************************************************************/
int ConnectDatabase()
{
SQLCHAR odbc[20];
SQLCHAR user[20];
SQLCHAR pass[20];
memset(odbc,0x0,20);
memset(user,0x0,20);
memset(pass,0x0,20);
GetConfig("ODBC",odbc);
GetConfig("USER",user);
GetConfig("PASS",pass);
Connectbl=FALSE;
/*分配资源*/
nResult= SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
if (nResult != SQL_SUCCESS && nResult != SQL_SUCCESS_WITH_INFO)
{
SQLFreeHandle(SQL_HANDLE_ENV,henv);
return(-1);
}
nResult = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
if (nResult != SQL_SUCCESS && nResult != SQL_SUCCESS_WITH_INFO)
{
SQLFreeHandle(SQL_HANDLE_ENV,henv);
return(-1);
}
nResult = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
if (nResult != SQL_SUCCESS && nResult != SQL_SUCCESS_WITH_INFO)
{
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
return(-1);
}
/*连接数据库*/
nResult = SQLConnect(hdbc,odbc,SQL_NTS,user,SQL_NTS,pass,SQL_NTS);
// if failed to connect, free the allocated hdbc before return
if (nResult == SQL_SUCCESS || nResult == SQL_SUCCESS_WITH_INFO)
{
Connectbl = TRUE;
return(1);
}
else
{
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);
return(-1);
}
}
/****************************************************************
* Function: DisConnectDB()
* puerpose:连接数据库
RESULT: 1:correct;-1:eror
*****************************************************************/
int DisConnectDB()
/*断开连接,释放资源*/
{
// if(SQL_SUCCESS != (nResult= SQLFreeHandle(SQL_HANDLE_STMT,hstmt)))
// MessageBox(0,"errors","test3",MB_OK);
if(SQL_SUCCESS != (nResult= SQLDisconnect(hdbc)))
return(-1);
if(SQL_SUCCESS != (nResult = SQLFreeHandle(SQL_HANDLE_DBC, hdbc)))
return(-1);
if(SQL_SUCCESS != (nResult= SQLFreeHandle(SQL_HANDLE_ENV,henv)))
return(-1);
return(1);
}
//
void GetConfig(char typed[],char target[]){
char buf[100];
FILE *cfPtr = fopen("odbc.con","r");
if (cfPtr==NULL){
strcpy("NULL",target);
exit(0);
}
while(!feof(cfPtr)){
fgets(buf,100,cfPtr);
replace(buf,"\n","");
if (!strncmp(buf,typed,(int)strlen(typed))){
strcpy(target,buf+(int)strlen(typed)+1);
exit(0);
}
}
}
int main(){
if (ConnectDatabase()!=1)
printf("Can't connection database.\nPlease check your ODBC configuration.\n\n");
else
printf("Connection right.\n");
DisConnectDB();
system("pause");
return 0;
}
就odbc能否在bcc 5.5下使用的问题,我特意email咨询过borland亚洲技术支持中心,我措辞是很恳切的,但一个星期了也没看他们答复,没办法,只有来这里碰碰运气了,希望各位高手不吝指教。