1.在stdAfx.h中导入:
#import "C:\\program files\\common files\\system\\ado\\msado15.dll" no_namespace rename ("EOF", "adoEOF")
2.在::InitInstance()中初始化:
BOOL CMYSQLApp::InitInstance() { CoInitialize(NULL); //初始化Com组件 //conPtr=NULL; //_ConnectionPtr conPtr; //数据库链接指针 conPtr.CreateInstance("ADODB.Connection"); //Connection用于与数据库服务器的链接 //conPtr.CreateInstance(__uuidof(Connection)); //Connection用于与数据库服务器的链接另一种方式 // MySQL ODBC 5.1 Driver为驱动名称(取决于我们为MySql安装的驱动),localhost为服务器地址,test为数据库名,root为用户名(MySql默认用户名为root),sa为密码 CString conStr=_T("Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=test1;"); /******************连接数据库********************/ try { conPtr->ConnectionTimeout = 5; //设置连接时间 conPtr->Open((_bstr_t)conStr, _T("root"),_T("root"), adModeUnknown); } catch(_com_error e) //捕捉异常 { AfxMessageBox(e.ErrorMessage()); } CoUninitialize(); //释放com组件 AfxEnableControlContainer();
3.增删改查语句:
void CMYSQLDlg::OnButton1() //查 { //CoInitialize(NULL); UpdateData(); // TODO: Add your control notification handler code here //_ConnectionPtr conPtr; //数据库链接指针 _RecordsetPtr recordPtr;//数据集指针 recordPtr.CreateInstance(__uuidof(Recordset)); CString cmdStr=_T("select * from aa");// //((CMYSQLApp*)AfxGetApp())-> try{ recordPtr->Open(_variant_t(cmdStr),((CMYSQLApp*)AfxGetApp())->conPtr.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); } catch(_com_error e) { e.Description(); } recordPtr->MoveFirst(); while (!(recordPtr->adoEOF)) {//获取记录 //CString name=(CString)recordPtr->GetCollect(_variant_t("id")).bstrVal; int no=recordPtr->GetCollect(_variant_t("id")).intVal; CString str; str.Format("%d",no); m_id=str; UpdateData(false); recordPtr->MoveNext(); //break; } /* recordPtr->Close();//关闭记录集 ((CMYSQLApp*)AfxGetApp())->conPtr->Close();//关闭连接 recordPtr.Release();//释放空间 ((CMYSQLApp*)AfxGetApp())->conPtr.Release();//释放空间*/ } void CMYSQLDlg::OnButton2() //删 { // TODO: Add your control notification handler code here UpdateData(); _RecordsetPtr recordPtr;//数据集指针 recordPtr.CreateInstance(__uuidof(Recordset)); int n; n=atoi(m_id); CString cmdStr; cmdStr.Format(_T("delete from aa where id=%d"),n);// try{ recordPtr->Open(_variant_t(cmdStr),((CMYSQLApp*)AfxGetApp())->conPtr.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); } catch(_com_error e) { e.Description(); } } void CMYSQLDlg::OnButton3() //、增 { // TODO: Add your control notification handler code here UpdateData(); _RecordsetPtr recordPtr;//数据集指针 recordPtr.CreateInstance(__uuidof(Recordset)); int n; n=atoi(m_id); CString cmdStr; cmdStr.Format(_T("insert into aa values(%d)"),n);// try{ recordPtr->Open(_variant_t(cmdStr),((CMYSQLApp*)AfxGetApp())->conPtr.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); } catch(_com_error e) { e.Description(); } } void CMYSQLDlg::OnButton4() //改 { // TODO: Add your control notification handler code here UpdateData(); _RecordsetPtr recordPtr;//数据集指针 recordPtr.CreateInstance(__uuidof(Recordset)); int n; n=atoi(m_id); //n=7; //CString cmdStr=_T("insert into aa values(%d)",m_dd);// CString cmdStr; cmdStr.Format(_T("update aa set id=%d where id=%d"),m_dd,n);// try{ recordPtr->Open(_variant_t(cmdStr),((CMYSQLApp*)AfxGetApp())->conPtr.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); } catch(_com_error e) { e.Description(); } }