国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

關于存儲過程的ADO調用的一些心得(輸出參數,返回值)

2024-07-21 02:06:18
字體:
來源:轉載
供稿:網友

在一個項目中,我需要用到存儲過程來訪問數據,為了提供一個比較一致的接口以便調用,我沒有使用createparameter(),而是調用commandptr的refresh()函數先從數據庫中查詢參數.
_connectionptr m_pconn;
m_pconn.createinstance(__uuidof(connection));
m_pconn->open("driver={sql server};server=127.0.0.1;database=pub;uid=sa;pwd=", "","",0);

_commandptr  m_pcommand;
m_pcommand.createinstance(__uuidof(command));
_recordsetptr m_precordset;
m_precordset.createinstance(__uuidof(recordset));

m_pcommand->activeconnection = m_pconn;
m_pcommand->commandtext = "sp_xx";//存儲過程名
m_pcommand->putcommandtype(adcmdstoredproc);
m_pcommand->parameters->refresh();//從數據庫查詢參數信息


接下來就可以對每一個參數賦值了:
long cnt = m_pcommand->parameters->getcount();//取得參數的個數
for(long k=1;k<cnt;k++)
{//由于ado中認為返回值是第一個參數,因此這里用k=1濾掉第一個參數
 m_pcommand->parameters->getitem(k)->value = xxx;//按存儲過程的參數順序給參數賦值
}


現在可以執行這個存儲過程了
m_precordset = m_pcommand->execute(0,0,adcmdstoredproc);
這個時候,如果接下來用
_variant_t ret_val = m_pcommand->parameters->getitem((long)0)->value;
那么將得不到值
而如果像下面這樣調用的話就可以得到返回值了
m_precordset->close();
_variant_t output_para = m_pcommand->parameters->getitem((long)0)->value;
ms ado.net給這一現象的回復是:
 you can think of a stored procedure as a function in your code. the function doesn’t return a value until it has executed all of its code. if the stored procedure returns results and you haven’t finished processing these results, the stored procedure hasn’t really finished executing. until you’ve closed the datareader, the return and output parameters of your command won’t contain the values returned by your stored procedure.
也就是說execute()函數應該看成是直到m_precordset關掉以后才會正確返回.

關于輸出參數的處理也和這一樣,因為返回值本身就是當成輸出參數來處理的.
通過這種方法,我們可以得到一個存儲過程的返回值和結果集,而且對于所有的存儲過程都可以一樣使用,不必為某個特定的存儲過程去寫代碼,具有一定的通用性.


收集最實用的網頁特效代碼!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武鸣县| 宁河县| 柯坪县| 万年县| 昌宁县| 布拖县| 湛江市| 方城县| 囊谦县| 开化县| 汕尾市| 永德县| 龙井市| 谷城县| 大田县| 铁岭市| 高清| 托克托县| 东宁县| 昭觉县| 平舆县| 龙山县| 洮南市| 讷河市| 平原县| 新田县| 江阴市| 民县| 庆城县| 霍邱县| 卓尼县| 封开县| 渝中区| 许昌县| 永城市| 曲靖市| 利辛县| 鄂温| 郴州市| 措勤县| 温泉县|