建一个ATL Service,然后加一个func.cpp,和func.h,
func.h定义是这样的:
#include "windows.h"
#include "sqlext.h"
#include "sql.h"
#include "sqltypes.h"
#include "iostream.h"
#include "string.h"
#include "stdio.h"
SQLHENV henv;
SQLHDBC hdbc;
SQLHSTMT hstmt;
SQLRETURN retcode;
void connectdatabase(char *datasource,char *id, char *password);
void ReadConfig(char *file, char* title, char* token, char* value);
void freehandl(char *hdbc_connect,char *hdbc_handle,char *henv);
void get_value(char *table,int row,char datavalue[][100]);
void all_func();
而func.cpp是这样的:
#include "func.h"
#include "stdafx.h"void connectdatabase(char *datasource,char *id, char *password)
{
retcode  =  SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv);
if(retcode == SQL_ERROR)
{
// cout<<"分配环境变量失败"<<endl;
}
else 
{
retcode = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(void *) SQL_OV_ODBC3,0);
if(retcode == SQL_ERROR)
{
// cout<<"设置环境变量失败"<<endl;
}
else
{
retcode = SQLAllocHandle (SQL_HANDLE_DBC,henv,&hdbc);
if(retcode == SQL_ERROR)
{
// cout<<"分配连接句柄失败"<<endl;
}
else
{
retcode = SQLConnect(hdbc,(SQLCHAR *)datasource,SQL_NTS,(SQLCHAR *)id,
SQL_NTS,(SQLCHAR *) password,SQL_NTS);
if(retcode == SQL_ERROR)
{
// cout<<"连接数据源失败"<<endl;
}
else
{
// cout<<"连接数据源成功,等待操作..."<<endl;
SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt);
}
/*
retcode = SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt);
if(retcode == SQL_SUCCESS||retcode == SQL_SUCCESS_WITH_INFO)
{
// cout<<"分配句柄成功"<<endl;
retcode = SQLExecDirect(hstmt,(SQLCHAR *)"select * from stdtable",SQL_NTS);
if(retcode == SQL_SUCCESS||retcode == SQL_SUCCESS_WITH_INFO)
{
cout<<"SQLExecDirect success"<<endl;
}
}
}
}
}
}
// SQLDisconnect(hdbc);
// SQLFreeHandle(SQL_HANDLE_ENV,henv);
}void ReadConfig(char *file, char* title, char* token, char* value)
{
char  tempstr[256] = {0};
FILE  *fp;

char strPath[256] = {0};
// strcpy(strPath, FilePath);
strcat(strPath, file);
if((fp=fopen(strPath,"r"))==NULL)
{
// cout<<"无法打开客户的配置文件,程序终止!"<<endl;
exit(1);
}

int len = strlen(title) +1;
fgets(tempstr, len, fp);
while(strcmp(tempstr, title) != 0)
{
if(feof(fp))
{
fclose(fp);
return;
}
fgets(tempstr, len, fp);
}

len = strlen(token) + 1;
fgets(tempstr, len, fp);
while(strcmp(tempstr, token) != 0)
{
if(feof(fp))
{
fclose(fp);
return;
}
fgets(tempstr, len, fp);
}

memset(tempstr, 0, 256);
char buf[8] = {0};
fread(buf, 1, 1, fp);
while(buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n')
{
strcat(tempstr, buf);
fread(buf, 1, 1, fp);
if(feof(fp))
{
break;
}
}
strcpy(value, tempstr);
// cout<<value<<endl;
fclose(fp);
return;
}
void freehandl(char *hdbc_connect,char *hdbc_handle,char *henv)
{
SQLFreeHandle(SQL_HANDLE_DBC,hdbc_handle);
SQLDisconnect(hdbc_connect);
SQLFreeHandle(SQL_HANDLE_ENV,henv);
}void get_value(char *table,int row,char datavalue[][100])
{
int i = 1;
long no;
char buffer[100];
// char datavalue[100][100];
char sql[100]="select * from ";
strcat(sql,table);
retcode=SQLExecDirect(hstmt,(SQLCHAR *)sql,SQL_NTS);
// cout<<sql<<endl;
if(retcode==SQL_ERROR)
{
// cout<<"取值语句执行失败"<<endl;
}
else
{
retcode=SQLBindCol(hstmt,row,SQL_C_CHAR,buffer,100,&no);
if(retcode==SQL_ERROR)
{
// cout<<"绑定失败"<<endl;
}
else
{
while(SQLFetch(hstmt)!=SQL_NO_DATA_FOUND)
{
strcpy(datavalue[i],buffer);
i++;
}
}
}
strcpy(datavalue[i],"end");
}void all_func()
{
int row=2,i=1;
char filename[10]="配置.ini",
data_table[100],
data_souce_name[10],
id[10],
password[10],
datavalue[100][100];
ReadConfig(filename,"[serverdata]","data_source_name=",data_souce_name);
ReadConfig(filename,"[serverdata]","data_table=",data_table);
ReadConfig(filename,"[serverdata]","data_id=",id);
ReadConfig(filename,"[serverdata]","data_password=",password);
connectdatabase(data_souce_name,id,password);
get_value(data_table,1,datavalue);
ReadConfig(filename,"[clientdata]","data_source_name=",data_souce_name);
}正常地,建一个控制台程序,再加一个cpp文件,在里面调用all_func()是成功的,但现在我在ATL Service中的Run函数调用all_func(),就不行了,提示有错误:--------------------Configuration: wuyang - Win32 Debug--------------------
Compiling...
func.cpp
D:\programfile\wuyang\func.cpp(5) : error C2065: 'SQLAllocHandle' : undeclared identifier
D:\programfile\wuyang\func.cpp(5) : error C2065: 'SQL_HANDLE_ENV' : undeclared identifier
D:\programfile\wuyang\func.cpp(5) : error C2065: 'SQL_NULL_HANDLE' : undeclared identifier
D:\programfile\wuyang\func.cpp(5) : error C2065: 'henv' : undeclared identifier
D:\programfile\wuyang\func.cpp(6) : error C2065: 'SQLSetEnvAttr' : undeclared identifier
D:\programfile\wuyang\func.cpp(6) : error C2065: 'SQL_ATTR_ODBC_VERSION' : undeclared identifier
D:\programfile\wuyang\func.cpp(6) : error C2065: 'SQL_OV_ODBC3' : undeclared identifier
D:\programfile\wuyang\func.cpp(7) : error C2065: 'SQL_HANDLE_DBC' : undeclared identifier
D:\programfile\wuyang\func.cpp(7) : error C2065: 'hdbc' : undeclared identifier
D:\programfile\wuyang\func.cpp(8) : error C2065: 'SQLConnect' : undeclared identifier
D:\programfile\wuyang\func.cpp(8) : error C2065: 'SQLCHAR' : undeclared identifier
D:\programfile\wuyang\func.cpp(8) : error C2059: syntax error : ')'
D:\programfile\wuyang\func.cpp(10) : error C2065: 'SQL_HANDLE_STMT' : undeclared identifier
D:\programfile\wuyang\func.cpp(10) : error C2065: 'hstmt' : undeclared identifier
D:\programfile\wuyang\func.cpp(124) : error C2065: 'SQLFreeHandle' : undeclared identifier
D:\programfile\wuyang\func.cpp(125) : error C2065: 'SQLDisconnect' : undeclared identifier
D:\programfile\wuyang\func.cpp(137) : error C2065: 'retcode' : undeclared identifier
D:\programfile\wuyang\func.cpp(137) : error C2065: 'SQLExecDirect' : undeclared identifier
D:\programfile\wuyang\func.cpp(137) : error C2059: syntax error : ')'
D:\programfile\wuyang\func.cpp(139) : error C2065: 'SQL_ERROR' : undeclared identifier
D:\programfile\wuyang\func.cpp(145) : error C2065: 'SQLBindCol' : undeclared identifier
D:\programfile\wuyang\func.cpp(145) : error C2065: 'SQL_C_CHAR' : undeclared identifier
D:\programfile\wuyang\func.cpp(152) : error C2065: 'SQLFetch' : undeclared identifier
D:\programfile\wuyang\func.cpp(152) : error C2065: 'SQL_NO_DATA_FOUND' : undeclared identifier
Generating Code...
Compiling...
wuyang.cpp
Generating Code...
Error executing cl.exe.wuyang.exe - 24 error(s), 0 warning(s)
我记得这些定义应该都是在odbc32.lib中,于是我在project->setting->link中加入了odbc32.lib,问题没解决.然后我在func.h中加入#include "odbcinst.h",错误依然,请问这是什么问题?