魔兽托尔本总部在那里:关于存储过程的ADO调用的一些心得(输出参数,返回值)-数据库专栏,SQL Server-...
来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 22:26:20
在一个项目中,我需要用到存储过程来访问数据,为了提供一个比较一致的接口以便调用,我没有使用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{//由于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关掉以后才会正确返回.
关于输出参数的处理也和这一样,因为返回值本身就是当成输出参数来处理的.
通过这种方法,我们可以得到一个存储过程的返回值和结果集,而且对于所有的存储过程都可以一样使用,不必为某个特定的存储过程去写代码,具有一定的通用性.
_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
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关掉以后才会正确返回.
关于输出参数的处理也和这一样,因为返回值本身就是当成输出参数来处理的.
通过这种方法,我们可以得到一个存储过程的返回值和结果集,而且对于所有的存储过程都可以一样使用,不必为某个特定的存储过程去写代码,具有一定的通用性.
关于存储过程的ADO调用的一些心得(输出参数,返回值)-数据库专栏,SQL Server-...
SQL SERVER 数值类型的存储格式及转换 - misterliwei的专栏 - CS...
C# 编写SQL SERVER 2005 的存储过程 - 9who - 博客园
SQL SERVER 存储过程学习笔记
IDL 连接sql server 2005 数据库心得
sql server2005 存储数据库表名、字段名、描述的数据库表和sql语句
Sql Server数据库的备份和恢复措施
SQL Server数据库日志已满的解决方法
实例操作 教你玩转SQL Server存储过程
完全卸载SQL Server 2005 - yongsheng0550的专栏 - CSDN...
SQL Server 数据库备份
转载:ADO连接SQL数据库
SQL SERVER的数据类型
谈谈数据从sql server数据库导入mysql数据库的体验-技术文章,在线教程,电脑教...
SQL IF..ELSE..在存储过程的使用规范
SQL Server 2008空间数据应用系列八:基于Bing Maps(Silverlight)的空间数据存储
SQL Server 2000数据库收缩
存储过程 简介 数据库
sql server 错误9003:LSN无效(日志扫描号无效),对数据库的修复
SQL Server 2005—数据库管理10个最重要的特点
ADO.NET的数据库访问小结
Sql Server数据库触发器实例讲解
运用SQL Server数据表分区优化数据库
SQL Server与MySQL数据库对比