一。在stdAfx.h中插入:
#import "c:\program files\common files\system\ado\msado15.dll" \
no_namespace \
rename ("EOF", "adoEOF")
#include <icrsint.h>
二。在APP类中声明m_pConnection,在InitInstance()中建立连接:
if (!AfxOleInit())
{
AfxMessageBox("错");
return FALSE;
}
m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb","","",adModeUnknown);
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
三。建立自己的绑定类:
class CBindData : public CADORecordBinding
{
public:
BEGIN_ADO_BINDING(CBindData)
ADO_VARIABLE_LENGTH_ENTRY2(1, adVarChar, m_ch_fname,
sizeof(m_ch_fname), m_ul_fnameStatus, true)
//ADO_VARIABLE_LENGTH_ENTRY2(2, adVarChar, m_ch_lname,
// sizeof(m_ch_lname), m_ul_lnameStatus, false)
END_ADO_BINDING()
public:
CHAR m_ch_fname[20];
// CHAR m_ch_lname[32];
ULONG m_ul_fnameStatus;
// ULONG m_ul_lnameStatus;
};
四。在view中声明m_pRecordset;在CZzView::OnInitialUpdate() 中打开记录集并绑定:
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class CBindData rs;
CString aa;
aa="SELECT * FROM DemoTable";
IADORecordBinding* picRs=NULL;
m_pRecordset.CreateInstance(__uuidof(Recordset));
if( theApp.m_pConnection->State )
{
m_pRecordset->Open((_bstr_t)aa,// 查询DemoTable表中所有字段 theApp.m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,adLockBatchOptimistic,adCmdText);
}
TESTHR(m_pRecordset->QueryInterface(__uuidof(IADORecordBinding),
(LPVOID*)&picRs));
TESTHR(picRs->BindToRecordset(&rs));
}
五。我想在view的OnLButtonDown中检验以下绑定的情况:
void CZzView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default // CString aa;
//aa =(LPCSTR)_bstr_t(m_pRecordset->Fields->Item[(long)0]->Value) ;
CBindData my;
AfxMessageBox(my.m_ch_fname);
CView::OnLButtonDown(nFlags, point);
}
六。这时出现的为什么总是乱字付?是那里出问题了?请大家指教!谢谢
另外我发现如果按上述方法绑定,那自定义的绑定类只能和一个m_pRecordset打开的记录集进行绑定,
我试图写一个可以通用的绑定类,(就是可以打开未知列数、未知列类型的m_pRecordset记录集的类)
但是,不知enum DataTypeEnum怎么转换成CHAR类型,请大家指教,感谢!
#import "c:\program files\common files\system\ado\msado15.dll" \
no_namespace \
rename ("EOF", "adoEOF")
#include <icrsint.h>
二。在APP类中声明m_pConnection,在InitInstance()中建立连接:
if (!AfxOleInit())
{
AfxMessageBox("错");
return FALSE;
}
m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb","","",adModeUnknown);
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
三。建立自己的绑定类:
class CBindData : public CADORecordBinding
{
public:
BEGIN_ADO_BINDING(CBindData)
ADO_VARIABLE_LENGTH_ENTRY2(1, adVarChar, m_ch_fname,
sizeof(m_ch_fname), m_ul_fnameStatus, true)
//ADO_VARIABLE_LENGTH_ENTRY2(2, adVarChar, m_ch_lname,
// sizeof(m_ch_lname), m_ul_lnameStatus, false)
END_ADO_BINDING()
public:
CHAR m_ch_fname[20];
// CHAR m_ch_lname[32];
ULONG m_ul_fnameStatus;
// ULONG m_ul_lnameStatus;
};
四。在view中声明m_pRecordset;在CZzView::OnInitialUpdate() 中打开记录集并绑定:
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class CBindData rs;
CString aa;
aa="SELECT * FROM DemoTable";
IADORecordBinding* picRs=NULL;
m_pRecordset.CreateInstance(__uuidof(Recordset));
if( theApp.m_pConnection->State )
{
m_pRecordset->Open((_bstr_t)aa,// 查询DemoTable表中所有字段 theApp.m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,adLockBatchOptimistic,adCmdText);
}
TESTHR(m_pRecordset->QueryInterface(__uuidof(IADORecordBinding),
(LPVOID*)&picRs));
TESTHR(picRs->BindToRecordset(&rs));
}
五。我想在view的OnLButtonDown中检验以下绑定的情况:
void CZzView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default // CString aa;
//aa =(LPCSTR)_bstr_t(m_pRecordset->Fields->Item[(long)0]->Value) ;
CBindData my;
AfxMessageBox(my.m_ch_fname);
CView::OnLButtonDown(nFlags, point);
}
六。这时出现的为什么总是乱字付?是那里出问题了?请大家指教!谢谢
另外我发现如果按上述方法绑定,那自定义的绑定类只能和一个m_pRecordset打开的记录集进行绑定,
我试图写一个可以通用的绑定类,(就是可以打开未知列数、未知列类型的m_pRecordset记录集的类)
但是,不知enum DataTypeEnum怎么转换成CHAR类型,请大家指教,感谢!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货