vc 6.0连接mysql_VC++6.0程序连接Oracle数据库

这篇博客介绍了如何在VC++6.0应用程序中使用ADO连接到MySQL和Oracle数据库。通过创建ADODB.Connection对象并设置连接字符串,实现数据库的打开和关闭操作。在遇到异常时,程序会捕获并显示错误信息。
摘要由CSDN通过智能技术生成

在MyAdoDB.cpp中添加以下  代码  我全部复制了

// MyAdoDB.cpp: implementation of the CMyAdoDB class.

//

//

#include "stdafx.h"

#include "MyAdoDB.h"

#ifdef _DEBUG

#undef THIS_FILE

static char THIS_FILE[]=__FILE__;

#define new DEBUG_NEW

#endif

//

// Construction/Destruction

//

CMyAdoDB::CMyAdoDB()

{

AfxOleInit();//初始化COM库

AfxEnableControlContainer();

}

CMyAdoDB::~CMyAdoDB()

{

}

----打开数据文件连接

BOOL CMyAdoDB::Connect(CString strConnStr)

{

HRESULT hr;

try

{

hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象

if(SUCCEEDED(hr))

{

_bstr_t strConnect = _bstr_t(strConnStr);

m_pConnection->ConnectionTimeout=30;///设置超时时间为30秒

//连接数据库

hr = m_pConnection->Open(strConnect,"","",adModeUnknown);

return TRUE;

}

}

catch(_com_error e)///捕捉异常

{

CString temp;

temp.Format("错误信息:%s",e.ErrorMessage());

MessageBox(NULL,temp, "失败", MB_OK);

return FALSE;

}

return FALSE;

}

----关闭数据库连接

BOOL CMyAdoDB::CloseConnection()

{

//以下是连接access2000数据库。。。

try

{

if(m_pConnection->State)

return m_pConnection->Close();

}

catch(_com_error e)///捕捉异常

{

CString temp;

temp.Format("错误信息:%s",e.ErrorMessage());

MessageBox(NULL, temp, "失败", MB_OK);

return FALSE;

}

return FALSE;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值