VC下使用数据库,用哪种方式可以不通过控件,直接通过对象实现? VC下使用数据库,用哪种方式可以不通过控件,直接通过对象实现的?使用CRecordset好象必须绑定控件,不知道DAO,OLE DB,ADO等能否不用控件直接读数据源。 求教!先谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ADO不用控件,有几个步骤 // 包含库文件#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")// 初始化COM支持CoInitialize(NULL);// 使用Connection和Recordset对象_ConnectionPtr pConnect;_RecordsetPtr pRecordset;// 打开Connection,连接SQLServer2Ktry{pConnect.CreateInstance(_uuidof(Connection));_bstr_t bstrCon = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=mydatabase;Data Source=(local)";pConnect->Open(bstrCon, "","",adoModeUnknown);}catch(_com_error e){AfxMessageBox(e.ErrorMessage());}// 打开Recordset,通过Connection连接取得记录集try{pRecordset.CreateInstance(_uuidof(Recordset));_bstr_t bstrSQL = "Select * from TableName";pRecordset->Open(bstrSQL, pConnect.GetInterfacePtr(),adOpenDynamic, adLockOptimistic, adCmdTable);}catch(_com_error e){AfxMessageBox(e.ErrorMessage());}// 遍历记录集try{ pRecordset->MoveFirst(); while(pRecordset->adoEOF != VARIANT_TRUE) { // 读字段 CString str = (char*)(_bstr_t)pRecordset->Fields->GetItem((_varaint_t)"姓名")->Value; // 写字段 pRecordset->Fields->GetItem((_varaint_t)"姓名")->Value = _bstr_t("无名氏"); pRecordset->Update(); pRecordset->MoveNext();}}catch(_com_error e){ AfxMessageBox(e.ErrorMessage()); } 谢谢楼上的!我也正在研究ADO,但发现你讲的步骤比书上讲的简单很多 以前总结的嘛,相当于一个备忘。至于细节,诸如函数参数意义,你得看书和MSDN了。 CRecordset不需要, CRecordset devSet(&PMAIN->m_database);//PMAIN->m_database定义好的CDatabase对象 CString strDevId; strDevId.Format("%d", PMAIN->m_ComDev.comData[i].dev_id); devSet.m_strFilter = "a.dev_tp_id = b.dev_tp_id and a.dev_id = " + strDevId; devSet.Open(CRecordset::snapshot, _T("select dev_tp_nm, dev_id from t_device a, t_dev_type b"), CRecordset::readOnly); if(devSet.GetRecordCount() <= 0) { AfxMessageBox("δÕÒµ½¸ÃÉ豸¶ÔÓ¦µÄÉ豸ÀàÐÍÐÅÏ¢!"); } 楼上的还是需要一个dialog吧?我是希望直接在SDI窗口上完成 有哪位知道成都易我科技这个公司怎么样?急!!!!! 用GetIconInfo得到的图标大小不对 给分贴! 如何拦截全局钩子,有没有什么好办法,最好有代码 怎么用程序实现显示当前的网络连接, 就是系统命令net use之后显示的内容 RUNTIME_CLASS(CSplitWndView)报错 输入法和特定字体的关联问题 大家有没有遇到过这样的同事?如果你遇到了,你会怎么办? 谁对音频传输熟悉?帮看代码,50元现金酬谢。 请问关于onkeydown的问题,谢谢!!! VC8使用Xtreme的链接错误.... 找不到rnaui.dll链接库,着急啊!!
不用控件,有几个步骤
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")// 初始化COM支持
CoInitialize(NULL);// 使用Connection和Recordset对象
_ConnectionPtr pConnect;
_RecordsetPtr pRecordset;
// 打开Connection,连接SQLServer2K
try{
pConnect.CreateInstance(_uuidof(Connection));
_bstr_t bstrCon = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;
Initial Catalog=mydatabase;Data Source=(local)";
pConnect->Open(bstrCon, "","",adoModeUnknown);
}
catch(_com_error e){
AfxMessageBox(e.ErrorMessage());
}
// 打开Recordset,通过Connection连接取得记录集
try{
pRecordset.CreateInstance(_uuidof(Recordset));
_bstr_t bstrSQL = "Select * from TableName";
pRecordset->Open(bstrSQL, pConnect.GetInterfacePtr(),
adOpenDynamic, adLockOptimistic, adCmdTable);
}
catch(_com_error e){
AfxMessageBox(e.ErrorMessage());
}
// 遍历记录集
try{
pRecordset->MoveFirst();
while(pRecordset->adoEOF != VARIANT_TRUE)
{
// 读字段
CString str = (char*)(_bstr_t)pRecordset->Fields->GetItem((_varaint_t)"姓名")->Value;
// 写字段
pRecordset->Fields->GetItem((_varaint_t)"姓名")->Value = _bstr_t("无名氏");
pRecordset->Update();
pRecordset->MoveNext();
}
}
catch(_com_error e)
{ AfxMessageBox(e.ErrorMessage()); }
CRecordset devSet(&PMAIN->m_database);//PMAIN->m_database定义好的CDatabase对象
CString strDevId;
strDevId.Format("%d", PMAIN->m_ComDev.comData[i].dev_id);
devSet.m_strFilter = "a.dev_tp_id = b.dev_tp_id and a.dev_id = " + strDevId;
devSet.Open(CRecordset::snapshot, _T("select dev_tp_nm, dev_id from t_device a, t_dev_type b"), CRecordset::readOnly);
if(devSet.GetRecordCount() <= 0)
{
AfxMessageBox("δÕÒµ½¸ÃÉ豸¶ÔÓ¦µÄÉ豸ÀàÐÍÐÅÏ¢!");
}