ADO学习(三)Command 对象
Command?對(duì)象
ADO?Command?對(duì)象用于執(zhí)行面向數(shù)據(jù)庫(kù)的一次簡(jiǎn)單查詢。此查詢可執(zhí)行諸如創(chuàng)建、添加、取回、刪除或更新記錄等動(dòng)作。如果該查詢用于取回?cái)?shù)據(jù),此數(shù)據(jù)將以一個(gè)?RecordSet?對(duì)象返回。這意味著被取回的數(shù)據(jù)能夠被?RecordSet?對(duì)象的屬性、集合、方法或事件進(jìn)行操作。
Command?對(duì)象的主要特性是有能力使用存儲(chǔ)查詢和帶有參數(shù)的存儲(chǔ)過(guò)程。
屬性
| 屬性 | 描述 |
| ActiveConnection | 設(shè)置或返回包含了定義連接或?Connection?對(duì)象的字符串。 |
| CommandText | 設(shè)置或返回包含提供者(provider)命令(如?SOL?語(yǔ)句、表格名稱或存儲(chǔ)的過(guò)程調(diào)用)的字符串值。默認(rèn)值為?""(零長(zhǎng)度字符串)。 |
| CommandTimeout | 設(shè)置或返回長(zhǎng)整型值,該值指示等待命令執(zhí)行的時(shí)間(單位為秒)。默認(rèn)值為?30。 |
| CommandType | 設(shè)置或返回一個(gè)?Command?對(duì)象的類型 |
| Name | 設(shè)置或返回一個(gè)?Command?對(duì)象的名稱 |
| Prepared | 指示執(zhí)行前是否保存命令的編譯版本(已經(jīng)準(zhǔn)備好的版本)。 |
| State | 返回一個(gè)值,此值可描述該?Command?對(duì)象處于打開、關(guān)閉、連接、執(zhí)行還是取回?cái)?shù)據(jù)的狀態(tài)。 |
方法
| 方法 | 描述 |
| Cancel | 取消一個(gè)方法的一次執(zhí)行。 |
| CreateParameter | 創(chuàng)建一個(gè)新的?Parameter?對(duì)象 |
| Execute | 執(zhí)行?CommandText?屬性中的查詢、SQL?語(yǔ)句或存儲(chǔ)過(guò)程。 |
集合
| 集合 | 描述 |
| Parameters | 包含一個(gè)?Command?對(duì)象的所有?Parameter?對(duì)象。 |
| Properties | 包含一個(gè)?Command?對(duì)象的所有?Property?對(duì)象。 |
m_pConnection.CreateInstance(__uuidof(Connection));//創(chuàng)建連接對(duì)象實(shí)例
try?????????????????
{
//?打開本地Access庫(kù)AddressBook.mdb
m_pConnection->Open("Provider=SQLOLEDB.1;Password=123456;Persist?Security?Info=True;User?ID=sa;Initial?Catalog=CAMPUS;Data?Source=192.168.0.102","","",adModeUnknown);
//存儲(chǔ)過(guò)程名
_bstr_t?storproc("procname");
_bstr_t?name("1002");
_CommandPtr?pCommand;
pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection=m_pConnection;
//指定CommandTexe屬性為存儲(chǔ)過(guò)程名
pCommand->CommandText=storproc;
//指定CommandType屬性為存儲(chǔ)過(guò)程類型
pCommand->CommandType=adCmdStoredProc;
//在給參數(shù)賦值前調(diào)用Refresh方法
pCommand->Parameters->Refresh();
//給存儲(chǔ)過(guò)程的參數(shù)賦值
pCommand->Parameters->Item[_variant_t((long)1)]->Value=_variant_t((LPCTSTR)name);
//執(zhí)行存儲(chǔ)過(guò)程,返回_RecordsetPtr類型指針,因?yàn)樵?/span>CommandText中指定了命令,在Execute方法中參數(shù)賦空值就可了
m_pRecordset=pCommand->Execute(NULL,NULL,adCmdStoredProc);
?
?
CString?SID,SNAME,TID;
_variant_t?vt;
while?(!m_pRecordset->adoEOF)
{
//SID=(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("SID"))->Value);
vt?=?m_pRecordset->GetCollect("SID");
if?(vt.vt?!=?VT_NULL)?SID=(LPCSTR)_bstr_t(vt);
?
SNAME=(LPCSTR)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("SNAME"))->Value);
TID=(LPCSTR)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("TID"))->Value);
?
gConsoleLog.WriteLogInfo("%s--->%s--->%s",?SID,SNAME,TID);
?
m_pRecordset->MoveNext();
}
}
catch(_com_error?e)
{
AfxMessageBox(e.Description());
return;
}
存儲(chǔ)過(guò)程多參數(shù)的實(shí)例
BOOL CTestProDlg::LoadTestProInfo()
{
?/*創(chuàng)建和數(shù)據(jù)庫(kù)的連接*/
?CString strErr??= _T("") ;
?CString strUser??= _T("SCOTT") ;//用戶名
?CString strPassword = _T("zzcan728") ;//密碼
?CString strDSN??= _T("ORCL") ;//DSN
?_CommandPtr?pCommand = NULL ;
?/*創(chuàng)建訪問(wèn)接口*/
?try
?{
??HRESULT hr = m_pConn.CreateInstance(__uuidof(Connection)) ;
??if (FAILED(hr))
???_com_raise_error(hr, NULL) ;
??CString strConnect ;
??strConnect.Format(_T("Provider=OraOLEDB.Oracle.1;Password=%s;Persist Security Info=True;PLSQLRSet=1;User ID=%s;Data Source=%s"), strPassword, strUser, strDSN) ;
??m_pConn->CommandTimeout = 5 ;
??m_pConn->ConnectionTimeout = 5 ;
??m_pConn->Open((_bstr_t)strConnect, "", "", 0) ;
??if (m_pConn == NULL)
???throw CString(_T("The m_pDBConnect haven't been created!")) ;
?}
?catch(_com_error){
??strErr = _T("數(shù)據(jù)庫(kù)訪問(wèn)接口配置錯(cuò)誤或數(shù)據(jù)庫(kù)連接失敗!");
??AfxMessageBox(strErr, MB_ICONEXCLAMATION) ;
??return FALSE ;
?}
?catch(...)
?{
??strErr = _T("建立數(shù)據(jù)訪問(wèn)接口失敗,程序無(wú)法繼續(xù)運(yùn)行!");
??AfxMessageBox(strErr, MB_ICONEXCLAMATION) ;
??return FALSE ;
?}
?/*訪問(wèn)數(shù)據(jù)庫(kù),執(zhí)行存儲(chǔ)過(guò)程*/
?//設(shè)置傳遞的參數(shù)
?HRESULT hr ;
?CString strCommand = _T("sp_pro7") ;
?_RecordsetPtr pRst ;
?if (m_pConn == NULL)
??AfxMessageBox(_T("數(shù)據(jù)庫(kù)連接錯(cuò)誤"), MB_ICONEXCLAMATION ) ;
?//ASSERT(m_pConn == NULL) ;
?if (m_pConn == NULL)
??AfxMessageBox(_T("數(shù)據(jù)庫(kù)連接錯(cuò)誤"), MB_ICONEXCLAMATION ) ;
?try
?{
??if (FAILED(hr = pRst.CreateInstance(__uuidof(Recordset))))//創(chuàng)建_RecordSet對(duì)象實(shí)例
???_com_issue_error(hr) ;
??if ((FAILED(hr = pCommand.CreateInstance(__uuidof(Command)))))//創(chuàng)建Command對(duì)象實(shí)例
???_com_issue_error(hr) ;
??pCommand->ActiveConnection = m_pConn ;//將建立的連接賦值給pCommand
??pCommand->CommandType = adCmdStoredProc ;
??pCommand->CommandText = (LPCTSTR)strCommand ;//命令字符串
??_ParameterPtr pBookID;//BOOK ID
??_ParameterPtr pBookName;//書名
??_ParameterPtr pBookPublish;//出版社
??pBookID.CreateInstance(__uuidof(Parameter)) ;
??pBookName.CreateInstance(__uuidof(Parameter)) ;
??pBookPublish.CreateInstance(__uuidof(Parameter)) ;
??pBookID = pCommand->CreateParameter("id",adInteger,adParamInput,20,(_variant_t)"10");//給參數(shù)設(shè)置各屬性
??pCommand->Parameters->Append(pBookID);//加入到Command對(duì)象的參數(shù)集屬性中
??pBookName = pCommand->CreateParameter("Name",adVarChar,adParamInput,20,(_variant_t)"sanguoyanyi");
??pCommand->Parameters->Append(pBookName);
??pBookPublish = pCommand->CreateParameter("publish",adVarChar,adParamInput,20, (_variant_t)"安徽出版社");
??pCommand->Parameters->Append(pBookPublish);
??pRst = pCommand->Execute(NULL, NULL, adCmdStoredProc) ;
??return TRUE ;
?}
?catch(_com_error e)
?{
??AfxMessageBox(e.Description()) ;
??m_pConn->Close() ;
??CoUninitialize();
??return FALSE ;
?}
?return FALSE ;
}
?
總結(jié)
以上是生活随笔為你收集整理的ADO学习(三)Command 对象的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: static_cast, dynamic
- 下一篇: __declspec(novtable)