|
一. 数据库的编程(ADO)
1. 要用ADO连接数据的头文件中加入 #import "c:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
2. // 初始化OLE/COM库环境 _ConnectionPtr m_pConnection; ::CoInitialize(NULL); char buf[256],server[256],datebase[256],uid[256],pwd[256];char str[255];GetCurrentDirectory(255,str);//获得当前目录strcat(str,"//dbinfo.ini");int len0=GetPrivateProfileString("dbinfo","Provider","No Text",buf,256,str);//读取配置文件中的数据库连接信息int len1=GetPrivateProfileString("dbinfo","server","No Text",server,256,str);int len2=GetPrivateProfileString("dbinfo","Database","No Text",datebase,256,str);int len3=GetPrivateProfileString("dbinfo","uid","No Text",uid,256,str);int len4=GetPrivateProfileString("dbinfo","pwd","No Text",pwd,256,str); try { // 创建Connection对象 m_pConnection.CreateInstance("ADODB.Connection"); // 设置连接字符串,必须是BSTR型或者_bstr_t类型 _bstr_t strConnect = "Provider="; strConnect =strConnect+buf; strConnect =strConnect+";Server="; strConnect =strConnect+server; strConnect =strConnect+";Database="; strConnect =strConnect+datebase; strConnect =strConnect+";uid="; strConnect =strConnect+uid; strConnect =strConnect+";pwd="; strConnect =strConnect+pwd; m_pConnection->Open(strConnect,"","",adModeUnknown);//连接到指定的数据库 } // 捕捉异常 catch(_com_error e) { // 显示错误信息 AfxMessageBox(e.Description()); }
3. 执行无返回值(记录集)的SQL语句,Insert Update等无返回值的语句 // Connection对象的Execute方法:(_bstr_t CommandText, // VARIANT * RecordsAffected, long Options ) // 其中CommandText是命令字串,通常是SQL命令。 // 参数RecordsAffected是操作完成后所影响的行数, // 参数Options表示CommandText的类型:adCmdText-文本命令;adCmdTable-表名 // adCmdProc-存储过程;adCmdUnknown-未知 m_pConnection->Execute(bstrSQL,NULL,adCmdText);
4. 执行有返回值(记录集)的SQL语句_RecordsetPtr m_pRecordset; // 创建记录集对象m_pRecordset.CreateInstance(__uuidof(Recordset));// 返回表中的记录m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);CListCtrl m_list1;//记录显示在CListCtrl中if(!m_pRecordset->adoEOF) //如果记录不为空{ while(!m_pRecordset->adoEOF) { m_list1.InsertItem(i,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ID")); m_list1.SetItemText(i,1,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("tintCallType")); m_list1.SetItemText(i,2,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("tintHangUp")); _variant_t vMaxid(m_pRecordset->GetCollect("strCallingNmb")); if(vMaxid.vt==VT_NULL) vMaxid=""; m_list1.SetItemText(i,3,(LPCTSTR)(_bstr_t)vMaxid); _variant_t vMaxid0(m_pRecordset->GetCollect("strCalledNmb")); if(vMaxid0.vt==VT_NULL) vMaxid0=""; m_list1.SetItemText(i,4,(LPCTSTR)(_bstr_t)vMaxid0); _variant_t vMaxid1(m_pRecordset->GetCollect("strOriginNmb")); if(vMaxid1.vt==VT_NULL) //此字段在数据库中为NULL值 vMaxid1=""; i++;//CListCtrl中的第i行显示此记录 m_pRecordset->MoveNext();//下一条记录 } } m_pRecordset->Close();//记录集读取完毕,关闭记录集
5. 执行带参数无返回值(一个或者多个记录集)的存储过程_CommandPtr m_pnewCommand; m_pnewCommand.CreateInstance("ADODB.Command"); m_pnewCommand->ActiveConnection=m_pConnection;m_pnewCommand->CommandType=adCmdStoredProc;m_pnewCommand->CommandText=_bstr_t("proc_deleteSMCBQueue");unsigned char newflag = 1;_ParameterPtr Pnewflag,PnewrecordID;Pnewflag.CreateInstance(__uuidof(Parameter));PnewrecordID.CreateInstance(__uuidof(Parameter)); CString newst="";newst.Format("%d",recordID);Pnewflag=m_pnewCommand->CreateParameter(_bstr_t("flag"),adTinyInt,adParamInput,1,(_variant_t)newflag);m_pnewCommand->Parameters->Append(Pnewflag); PnewrecordID=m_pnewCommand->CreateParameter(_bstr_t("recordID"),adInteger,adParamInput,4,(_variant_t)(_bstr_t)newst);m_pnewCommand->Parameters->Append(PnewrecordID); try{ m_pnewCommand->Execute(NULL,NULL,adCmdStoredProc);}catch(...){ }
6. 执行有参数有返回记录集的存储过程(其实就是执行有返回值(记录集)的SQL语句)CString sqlrecord ="exec proc_getSMCBQueue 1";try{ m_pRecordset = m_pConnection->Execute((_bstr_t)sqlrecord,NULL,adCmdText);}catch(...){}
今天就记一下VC关于数据库的编程吧,下次再记VC关于网络的编程吧!
只是真正的用_CommandPtr m_pnewCommand执行存储过程返回一个或者多个记录集,再分别对各记录集进行操作还没有实现啊! |
|