如何使用VC++通过ADO访问数据库,并能返回记录数。请高手指点! pVal = (IDispatch**)&pRs;这样改编译会过,不知能不能得到你想要的结果。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 _ConnectionPtr pConn; _RecordsetPtr pRs;是你访问数据库的变量,一般你只访问一个数据库,所以,这两个变量你可以定义为全局的变量,因为你一定只有一个与数据库的连接。pRs针对不同的操作再进行更新。 STDMETHODIMP CUser::UserOpen(IDispatch **pVal){ AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here pConn.CreateInstance("ADODB.Connection"); char buf[100]; CString connectionstring,username,password,sql; connectionstring="Provider=SQLOLEDB;Data Source="; GetPrivateProfileString("System","ServerName","",buf,99,"c:\\BoTongOA.ini"); connectionstring+=CString(buf)+";Initial Catalog="; GetPrivateProfileString("System","DatabaseName","",buf,99,"c:\\BoTongOA.ini"); connectionstring+=CString(buf); GetPrivateProfileString("System","UserName","",buf,99,"c:\\BoTongOA.ini"); username=CString(buf); GetPrivateProfileString("System","Password","",buf,99,"c:\\BoTongOA.ini"); password=CString(buf); pConn->Open(_bstr_t(connectionstring),_bstr_t(username),_bstr_t(password),0); sql="select * from OF_DEPT"; pRs=pConn->Execute(_bstr_t(sql),&vRowsAffected,adCmdText); *pVal=(IDispatch*)pRs; pRs->Close; pConn->Close; return S_OK;}以上代码运行是正确的,但在VB调用其DLL时,其返回的RecordCount为-1,请问应该如何修改才可以使RecordCound返回其总的记录数。 sql="select * from OF_DEPT";pRs=pConn->Execute(_bstr_t(sql),&vRowsAffected,adCmdText);pRs->MoveLast(); //add this and try*pVal=(IDispatch*)pRs; 因为你的ado缺省是不能这样用的,她不支持行集的反取(也就是不能是forward-only)在它的open中的参数可以设置 CImage如何加载一张Png到对话框的背景上? 问一个关于QQ游戏大厅界面的问题 关于印章识别 crazy_lazy_pig请进 java或c++不知應選擇哪一種? 在msdn中,如果想找找是否有某种功能的函数,应该如何找? 怎样在程序中注册一个控件,比如说实现命令行键入regsvr32 mscomm32.ocx的功能?或者有其他好的方法来注册mscomm控件 求教大侠指点一个大难题?? 请问怎么向不属于同一进程的同名窗口发送消息? MFC调用动态链接库出现崩溃的情况 类里的变量,线程获取不到? 有谁能详细谈谈子类化和超类化的问题??(来者有分) 怎样回收无模式对话框创建后使用的内存!
_RecordsetPtr pRs;
是你访问数据库的变量,一般你只访问一个数据库,所以,这两个变量你可以定义为全局的变量,因为你一定只有一个与数据库的连接。pRs针对不同的操作再进行更新。
{
AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here
pConn.CreateInstance("ADODB.Connection");
char buf[100];
CString connectionstring,username,password,sql;
connectionstring="Provider=SQLOLEDB;Data Source=";
GetPrivateProfileString("System","ServerName","",buf,99,"c:\\BoTongOA.ini");
connectionstring+=CString(buf)+";Initial Catalog=";
GetPrivateProfileString("System","DatabaseName","",buf,99,"c:\\BoTongOA.ini");
connectionstring+=CString(buf);
GetPrivateProfileString("System","UserName","",buf,99,"c:\\BoTongOA.ini");
username=CString(buf);
GetPrivateProfileString("System","Password","",buf,99,"c:\\BoTongOA.ini");
password=CString(buf); pConn->Open(_bstr_t(connectionstring),_bstr_t(username),_bstr_t(password),0); sql="select * from OF_DEPT";
pRs=pConn->Execute(_bstr_t(sql),&vRowsAffected,adCmdText);
*pVal=(IDispatch*)pRs;
pRs->Close;
pConn->Close;
return S_OK;
}以上代码运行是正确的,但在VB调用其DLL时,其返回的RecordCount为-1,请问应该如何修改才可以使RecordCound返回其总的记录数。
pRs=pConn->Execute(_bstr_t(sql),&vRowsAffected,adCmdText);
pRs->MoveLast(); //add this and try
*pVal=(IDispatch*)pRs;
在它的open中的参数可以设置