1、 用import导入ADO 的 COM 文件msado15.dll
1
#import
"
C:\Program Files\Common Files\System\ADO\msado15.dll
"
\no_namespace

2、COM 使用时初始化
1
HRESULT ComInit()
2
{
3
HRESULT hr = S_OK; // 默认返回值
4
if FAILED(CoInitialize(NULL)) // COM 初始化调用
5
{
6
CoUninitialize();
7
hr = E_UNEXPECTED;
8
}
9
return hr;
10
}

2



3

4

5



6

7

8

9

10

3、建立数据库连接
1
HRESULT ConnectToDB( LPSTR pUserId ,
//
用户名
2
LPSTR pConnString,
//
连接字串
3
LPSTR pUserPassword ,
//
用户密码
4
ConnectOptionEnum ConnectOption)
//
连接参数
5
{
6
HRESULT hr = S_OK; // 默认返回值
7
_ConnectionPtr ptrConn; // 定义Connection对象
8
try
9
{
10
// 创建一个连接实体
11
hr = ptrConn.CreateInstance(__uuidof(Connection));
12
// 设定连接等待的最大秒数,默认是15秒
13
ptrConn->ConnectionTimeout = 20
14
// 打开连接
15
hr = ptrConn->Open(pConnString,
16
pUserId,
17
pUserPassword,
18
ConnectOption);
19
return hr;
20
}
21
catch(_com_error &pComError)
22
{
23
…… // 错误处理
24
return E_UNEXPECTED;
25
}
26
}
27
_RecordsetPtr GetRecordSet(LPSTR strSql, _ConnectionPtr ptrConn)
28
{
29
try
30
{
31
RecordsetPtr ptrRS; // recordset 对象
32
// 创建recordset 对象实体
33
ptrRS.CreateInstance(__uuidof(Recordset));
34
ptrRS->Open( strSql,
35
ptrConn.GetInterfacePtr(),
36
adOpenForwardOnly,
37
adLockUnspecified,
38
adCmdText);
39
或者
40
ptrRS = ptrConn ->Execute(m_ strSql,NULL, adCmdText);
41
return ptrRS;
42
}
43
catch(_com_error &a_pComError)
44
{
45
….// 错误处理
46
return NULL;
47
}
48
}

2

3

4

5



6

7

8

9



10

11

12

13

14

15

16

17

18

19

20

21

22



23

24

25

26

27

28



29

30



31

32

33

34

35

36

37

38

39

40

41

42

43

44



45

46

47

48

4、通过数据集(recordset)得到列的名称
1
HRESULT GetColumnNames(
2
_RecordsetPtr ptrRs,
//
recordset 对象
3
char
strColNames[][
255
],
4
DataTypeEnum iColTypes[])
5
{
6
try
7
{ // 参数变量
8
_variant_t l_vaIndex;
9
l_vaIndex.vt = VT_I2;
10
// COLUMNS总数
11
long lColCount;
12
lColCount = ptrRs ->Fields->Count;
13
// 循环取得列的属性和名称
14
for(int iIndex = 0 ; iIndex < lColCount; iIndex++)
15
{
16
l_vaIndex.iVal = iIndex; // 设置循环索引
17
// 取得字段名称
18
sprintf(strColNames[iIndex], "%s",(LPSTR)ptrRs ->Fields->GetItem(l_vaIndex)->Name);
19
// 取得字段属性
20
iColTypes = ptrRs ->Fields->GetItem(l_vaIndex)->Type;
21
}
22
}
23
return S_OK;
24
}
25
catch
(_com_error
&
a_pComError)
26
{
27
…. // 错误处理
28
return E_UNEXPECTED;
29
}
30
catch
(
)
31
{
32
…. // 错误处理
33
return E_UNEXPECTED;
34
}
35
}

2

3

4

5



6

7



8

9

10

11

12

13

14

15



16

17

18

19

20

21

22

23

24

25

26



27

28

29

30


31



32

33

34

35

5、通过数据集(recordset)得到当前行记录
1
HRESULT getOneRecord(
2
_RecordsetPtr ptrRs,
3
const
long
lNoOfColumns,
4
_variant_t varValue[])
5
{
6
try
7
{
8
// 参数变量
9
_variant_t l_vaIndex;
10
l_vaIndex.vt = VT_I2;
11
// 循环取得列的值
12
for(long lIndex = 0; lIndex < lNoOfColumns; lIndex ++)
13
{
14
l_vaIndex.iVal = lIndex;
15
// 取得字段值
16
varValue[lIndex]= ptrRs->Fields->GetItem(l_vaIndex)->Value;
17
}
18
return S_OK;
19
}
20
catch(_com_error &a_pComError)
21
{
22
…. // 错误处理
23
return E_UNEXPECTED;
24
}
25
catch(
)
26
{
27
…. // 错误处理
28
return E_UNEXPECTED;
29
}
30
}

2

3

4

5



6

7



8

9

10

11

12

13



14

15

16

17

18

19

20

21



22

23

24

25


26



27

28

29

30

6、出错情况下错误信息的取得
1
void
ErrorFunc(_com_error
&
pComError, _ConnectionPtr ptrConn);
2
{
3
// COM 错误取得
4
// 当执行COM功能的时候,如果出错,可以捕捉到_com_error的异常
5
char lpComErrorStr512];
6
sprintf(lpComErrorStr512,"ErrorCode = %08lx \
7
Error Message = %s \
8
Source = %s \
9
Description = %s ",
10
pComError.Error(), // 错误编号
11
pComError.ErrorMessage(),// 错误信息
12
(LPCSTR) pComError.Source(),// 错误源
13
(LPCSTR) pComError.Description());// 错误描述
14
// 通过上面的代码我们可以看出,_com_error对象中可以得到COM所有出错的信息
15
// ADO错误取得
16
ErrorPtr pErr = NULL;
17
if( (ptrConn ->Errors->Count) > 0)
18
{
19
long nCount = ptrConn ->Errors->Count;
20
for(long i = 0; i < nCount; i++)
21
{
22
pErr = a_pConnPtr->Errors->GetItem(i);
23
char l_pchErrorString[512];
24
sprintf(l_pchErrorString,"Error:\n Error number: %x\t%s",
25
pErr->Number, // 错误编号
26
pErr->Description); // 错误描述
27
}
28
}
29
// ADO 处理出错的情况下, 在connection对象里面都有记录,可以通过访问
30
// connection 对象取得错误编号和错误信息。

2



3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18



19

20

21



22

23

24

25

26

27

28

29

30
