2. 安装myodbc-3.51.11-2-win.exe,文件夹中有
3. 建立数据源
附ADOLinkMySQL.sql-----------------------------------------------------------------------------------------------建立数据库ADOLinkMySQL--建立表ADOLinkMySQL--ADOLinkMySQL表插入数据--2006-05-11--archfree---------------------------------------------------------------------------------------------drop database if exists `ADOLinkMySQL`;create database `ADOLinkMySQL`;use ADOLinkMySQL;drop table if exists `ADOLinkMySQL`;create table `ADOLinkMySQL`(`Name` varchar(255)not null comment'姓名',`Age` int(200) unsigned not null comment'年',`Sex` varchar(255) not null comment'性别')Engine=InnoDB default charset=gb2312;insert into `ADOLinkMySQL`(`Name`,`Age`,`Sex`)values('Jack','20','boy'),('Rose','19','girl');-----------------------------------------------------------------------------------------------
4.利用向导生成基于对话框程序ADOLinkMySQL
5.添加如下代码:
在StdAfx.h文件
//宏定义错误
#if !defined CATCH_ERROR
#define CATCH_ERROR { CString strComError; strComError.Format("错误编号: %081x\n错误信息: %s\n错误源: %s\n错误描述: %s",e.Error(),e.ErrorMessage(),(LPCSTR)e.Source(),(LPCSTR)e.Description());::MessageBox(NULL,strComError,"错误",MB_ICONEXCLAMATION);}
#endif
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
//引入ADO类型库
/*导入库文件使用ADO前必须在工程的stdafx.h文件最后用直接引入符号#import引入ADO库文件,以使编译器能正确编译。代码如下:#import "C:\Program Files\common files\system\ado\msado15.dll" no_namespace rename("EOF","EndOfFile") rename("BOF","FirstOfFile")ADO类的定义是作为一种资源存储在ADO DLL(msado15.dll)中,在其内部称为类型库。类型库描述了自治接口,以及C++使用的COM vtable接口。 当使用#import指令时,在运行时Visual C++需要从ADO DLL中读取这个类型库,并以此创建一组C++头文件。 这些头文件具有.tli 和.tlh扩展名,读者可以在项目的目录下找到这两个文件。在C++程序代码中调用的ADO类要在这些文件中定义。 程序的第三行指示ADO对象不使用名称空间。在有些应用程序中,由于应用程序中的对象与ADO中的对象之间可能会出现命名冲突, 所以有必要使用名称空间。如果要使用名称空间,则可把第三行程序修改为: rename_namespace("AdoNS")。 第四行代码将ADO中的EOF(文件结束)更名为adoEOF,以避免与定义了自己的EOF的其他库冲突。*/
ADOLinkMySQLDlg.cpp
BOOL CADOLinkMySQLDlg::OnInitDialog()
{
/* */
CDialog::OnInitDialog();::SQLConfigDataSource(0,4,"MySQL ODBC 3.51 Driver\0","DSN=ADOLinkMySQL\0DATABASE=ADOLinkMySQL\0SERVER=192.168.1.52\0UID=root\0PWD=```");
DWORD dwStyle;
dwStyle = m_listResult.GetStyle();
dwStyle = LVS_EX_FULLROWSELECT ; m_listResult.SetExtendedStyle(dwStyle);
m_listResult.InsertColumn(0,"姓名");
m_listResult.InsertColumn(1,"年龄");
m_listResult.InsertColumn(2,"性别");
RECT rect; m_listResult.GetWindowRect(&rect);
int wid=rect.right-rect.left;
m_listResult.SetColumnWidth(0,wid/3);
m_listResult.SetColumnWidth(1,wid/3);
m_listResult.SetColumnWidth(2,wid/3);
/*初始化COM环境 (1)::CoInitialize(NULL); //初始化OLE/COM库环境 ::CoUninitialize();//既然初始化了环境,当然就有必要释放他了 (2)也可以调用MFC全局函数 AfxOleInit();*/AfxOleInit(); //初始化COM库
_ConnectionPtr m_pConnection;//添加一个指向Connection对象的指针try
{
HRESULT hr=m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
if(SUCCEEDED(hr))
{hr=m_pConnection->Open("DSN=ADOLinkMySQL;"/*数据源名称*/, "root",/*用户*/ "```",/*密码*/0);//连接数据库
}
}
catch(_com_error e)//COM错误取得,当执行COM功能的时候,如果出错,可以捕捉到_com_error的异常
{CATCH_ERROR;
return false;
}
MessageBox("连接成功");_RecordsetPtr m_pRecordset; //定义一个指向Recordset对象的指针 m_pRecordset.CreateInstance("ADODB.Recordset"); //创建Recordset对象的实例try
{
_variant_t vName,vAge,vSex;
m_pRecordset->Open("select * from ADOLinkMySQL",_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
while(!m_pRecordset->adoEOF)
{ vName=m_pRecordset->GetCollect("Name");
vAge=m_pRecordset->GetCollect("Age");
vSex=m_pRecordset->GetCollect("Sex");
int i=0;
m_listResult.InsertItem(i,(LPCTSTR)(_bstr_t)vName);
m_listResult.SetItemText(i,1,(LPCTSTR)(_bstr_t)vAge);
m_listResult.SetItemText(i,2,(LPCTSTR)(_bstr_t)vSex);
i++;m_pRecordset->MoveNext(); //移到下一条记录
}
}
catch(_com_error e)
{
CATCH_ERROR;
return false;
}m_pRecordset->Close();//关闭记录集 m_pConnection->Close();//关闭连接
// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < psysmenu =" GetSystemMenu(FALSE);">
if (!strAboutMenu.IsEmpty())
{ pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7338284/viewspace-199266/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/7338284/viewspace-199266/