使用ODBC API之前要用到的头文件和LIB库
#include "sql.h" // This is the the main include for ODBC Core functions.
#include "sqlext.h" // This is the include for applications using the Microsoft SQL Extensions
#include "sqltypes.h" // This file defines the types used in ODBC
#include "sqlucode.h" // This is the the unicode include for ODBC Core functions
#include "odbcss.h" // This is the application include file for the SQL Server driver specific defines.
#pragma coment(lib, "odbc32.lib")
这些头文件在vc安装目录的INCLUDE里面都有。
但是如果就这样直接使用这些头文件,在编译的时候会出现如下错误:
error C2146: syntax error : missing ';' before identifier 'SQLHWND'
fatal error C1004: unexpected end of file found
解决办法:在最前面加上#inlcude <windows.h> 因为sql.h中引用了windows.h的一些定义方法
这样,就可以使用ODBC API直接进行数据库操作了。而不使用MFC的相关类。
动态加载数据源:
通过ODBC API加载:Windows系统子目录下的动态链接库Odbcinst.dll提供了一个可以动态增加、修改和删除数据源的函数SQLConfigDataSource,由于VC的默认库文件中不包含此函数,因此使用前需将Odbcinst.h文件包含在工程的头文件中,在工程的setting属性框Link页的Object/library module编辑框中增加Odbc32.lib,同时保证系统目录system32下有文件Odbccp32.dll。