VC++6.0程序连接Oracle数据库
1、新建一个MFC 窗口程序

2、选择对话框程序

3、什么都不用勾选 直接下一步

4、选择静态库

5、删除初始的 控件

6、添加按钮 并且设置按钮名字:

7、双击按钮,添加按钮的功能.

8、打开类视图 添加一个新的类

9、类名字为CMyAdoDB

10、在头文件MyAdoDB.h中添加以下代码:
#include "StdAfx.h"
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
在类中添加 声明:
BOOL CloseConnection();
BOOL Connect(CString strSourceName="");
_ConnectionPtr m_pConnection;

11、在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;
}

12、在LinkOracleDemoDlg.cpp中添加全局变量
CMyAdoDB theADOConn;

13、在按钮 连接 的代码中添加
CString DBcon = "Provider=OraOLEDB.Oracle.1;Password=wzhwzh199126;Persist Security Info=True;User ID=SYSTEM;Data Source=ORCL";
MessageBox(DBcon);
//建立与数据库的链接,程序退出再释放
if((theADOConn.Connect(DBcon)) != TRUE)
{
MessageBox("数据库连接错误","数据库连接错误",MB_OK);
return ;
}

14、在关闭连接中添加:
theADOConn.CloseConnection();

15、运行
