最近拿了一个开源的源码看了下,在调试的过程中发现调用存数过程的output参数的时候一直出错,现在将问题记录下来。

问题描述:

1. 使用Microsoft.Practices.EnterpriseLibrary.Data.dll调用数据库

2. 存数过程如下:

USE [Survey]
GO
/****** Object:  StoredProcedure [dbo].[vts_spQuestionCopy]    Script Date: 08/03/2014 19:11:10 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
/*
/// <summary>
/// Copy an existing question to another survey
/// </summary>
*/
ALTER PROCEDURE [dbo].[vts_spQuestionCopy]@QuestionID int, @NewSurveyID int,@DisplayOrder int,@PageNumber int,@QuestionCopyID int output
ASBEGIN TRANSACTION CopyQuestionINSERT INTO vts_tbQuestion  (ParentQuestionId, SurveyID,LibraryID,SelectionModeId, LayoutModeId, DisplayOrder,PageNumber, MinSelectionRequired, MaxSelectionAllowed, RatingEnabled,ColumnsNumber,RandomizeAnswers,QuestionText,QuestionPipeAlias,QuestionIDText,HelpText,Alias,QuestiongroupID,ShowHelpText)
SELECT      ParentQuestionId, @NewSurveyID,null, SelectionModeId, LayoutModeId, @DisplayOrder,@PageNumber, MinSelectionRequired, MaxSelectionAllowed, RatingEnabled,ColumnsNumber,RandomizeAnswers,QuestionText,QuestionPipeAlias,QuestionIDText,HelpText,Alias,QuestionGroupID,ShowHelpText
FROM vts_tbQuestion WHERE QuestionId = @QuestionID-- Check if the cloned question was created
IF @@rowCount <> 0
BEGIN-- Clone the question's answersset @QuestionCopyID = convert(int,Scope_Identity())INSERT INTO vts_tbMultiLanguageText(LanguageItemID, LanguageCode, LanguageMessageTypeID, ItemText)SELECT @QuestionCopyID as LanguageItemID, LanguageCode, LanguageMessageTypeID, ItemTextFROM vts_tbMultiLanguageTextWHERE LanguageItemID = @QuestionID AND LanguageMessageTypeID in(3,10,11,12)    exec vts_spQuestionChildsClone @QuestionID, @QuestionCopyID, @NewSurveyIDUPDATE vts_tbQuestion SET DisplayOrder = @DisplayOrder, PageNumber = @PageNumber WHERE SurveyID = @NewSurveyID AND ParentQuestionid = @QuestionCopyIDexec vts_spAnswersCloneByQuestionId @QuestionID, @QuestionCopyIDexec vts_spQuestionSectionOptionClone @QuestionID, @QuestionCopyID-- Update the display orderUPDATE vts_tbQuestion SET DisplayOrder = DisplayOrder + 1 WHERE SurveyID = @NewSurveyID AND((QuestionID<>@QuestionCopyID AND ParentQuestionID is null) OR(ParentQuestionID is not null AND ParentQuestionID <> @QuestionCopyID)) ANDDisplayOrder >= @DisplayOrder
ENDCOMMIT TRANSACTION CopyQuestion

3. 代码中的调用过程如下:

        public int CopyQuestionById(int questionId, int targetSurveyId, int targetDisplayOrder, int targetPageNumber){//SqlParameter[] commandParameters = new SqlParameter[] //{ new SqlParameter("@QuestionId", questionId), //    new SqlParameter("@NewSurveyId", targetSurveyId), //    new SqlParameter("@DisplayOrder", targetDisplayOrder), //    new SqlParameter("@PageNumber", targetPageNumber), //    new SqlParameter("@QuestionCopyId", SqlDbType.Int) //};//commandParameters[4].Direction = ParameterDirection.Output;
ArrayList commandParameters = new ArrayList();{commandParameters.Add(new SqlParameter("@QuestionId", questionId).SqlValue);commandParameters.Add(new SqlParameter("@NewSurveyId", targetSurveyId).SqlValue);commandParameters.Add(new SqlParameter("@DisplayOrder", targetDisplayOrder).SqlValue);commandParameters.Add(new SqlParameter("@PageNumber", targetPageNumber).SqlValue);commandParameters.Add(new SqlParameter("@QuestionCopyId", SqlDbType.Int) { Direction = ParameterDirection.Output}.SqlValue);}DbConnection.db.ExecuteNonQuery("vts_spQuestionCopy", commandParameters);return int.Parse(commandParameters[4].ToString());}

咱们来分析一下这段代码:在调用的使用一直提示一个错误

new SqlParameter("@QuestionCopyId", SqlDbType.Int) { Direction = ParameterDirection.Output}.SqlValue

为空。原来当为Output时,sqlvalue就会为空,现在我们应该怎么办呢?

我尝试给其一个默认值,将代码修改为

new SqlParameter("@QuestionCopyId", SqlDbType.Int) { Direction = ParameterDirection.Output, Value = 0}.SqlValue

新的问题出现了:

An exception of type 'System.InvalidOperationException' occurred in Microsoft.Practices.EnterpriseLibrary.Data.dll but was not handled in user code

Additional information: The number of parameters does not match number of values for stored procedure. 

ExecuteNonQuery(string storedProcedureName, params object[] parameterValues)

这个错误是什么意思呢?意思是说参数个数不一致。查询后得知,原来 params 传的是值,当Output有默认值的时候,传入参数就为5个,可是存储过程只接受4个。现在应该怎么办呢?

尝试修改

DbConnection.db.ExecuteNonQuery("vts_spQuestionCopy", commandParameters.ToArray());

但是还是会报错,同样的错误。尝试去掉默认值,使用上述方法,成功运行,但是想取到返回值就懵了,取不到。肿么办?

尝试使用ExecuteNonQuery(DbCommand command),将代码修改如下:

        public int CopyQuestionById(int questionId, int targetSurveyId, int targetDisplayOrder, int targetPageNumber){SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@QuestionId", questionId), new SqlParameter("@NewSurveyId", targetSurveyId), new SqlParameter("@DisplayOrder", targetDisplayOrder), new SqlParameter("@PageNumber", targetPageNumber), new SqlParameter("@QuestionCopyId", SqlDbType.Int) };commandParameters[4].Direction = ParameterDirection.Output;SqlCommand vts_spQuestionCopy = new SqlCommand("vts_spQuestionCopy");vts_spQuestionCopy.CommandType = CommandType.StoredProcedure;vts_spQuestionCopy.Parameters.AddRange(commandParameters);DbConnection.db.ExecuteNonQuery(vts_spQuestionCopy);var result =int.Parse(vts_spQuestionCopy.Parameters["@QuestionCopyID"].Value.ToString());return result;}

运行成功,取到output返回值。

Note:ExecuteNonQuery(string storedProcedureName, params object[] parameterValues)中的params其实是将SqlParameter中的值传入。想要取到output返回值的时候,可能会存在问题。目前我是使用ExecuteNonQuery(DbCommand command)来得到返回值,各位如果有其它方法,敬请留下,谢谢。

转载于:https://www.cnblogs.com/aaronday/p/3888909.html

使用Microsoft.Practices.EnterpriseLibrary.Data调用存数过程Output参数注意事项相关推荐

  1. webservice mysql配置文件_在WebService中使用Microsoft.Practices.EnterpriseLibrary.Data配置数据库...

    标签: 1. 新建WebApplication1项目 1.1 新建-Web-ASP.NET Empty Web Application--WebApplication1 1.2 添加一个WebForm ...

  2. Microsoft.Practices.EnterpriseLibrary.Data 数据库操作

    出处:Microsoft.Practices.EnterpriseLibMicrosoft.Practices.EnterpriseLibrary.Data 数据库操作 - 米高佐敦 - 博客园Mic ...

  3. 解决: 未能加载文件或程序集“Microsoft.Practices.EnterpriseLibrary.Data,

    在文件夹中找到这几个文件,替换成合适的版本即可

  4. WCF-004:WCF中也可以使用Microsoft.Practices.EnterpriseLibrary

    在WCF服务中我一直是使用ADO.NET 实体模型来连接数据和操作数据库的,刚开始用时觉得比较新鲜,因为linq与sql语句的基本操作都有对应的使用方法,而且能直接获取到数据表的模型,大多情况下还是比 ...

  5. Microsoft.Practices.EnterpriseLibrary连接Oracle

    Microsoft Enterprise Library用起来也是挺不错的,对于习惯自己写sql语句的开发者们,这个应该挺好的.下面以连接Oracle为例简单介绍下如何连接数据库. 首先下载Micro ...

  6. Microsoft.Practices.EnterpriseLibrary

    项目中使用了Microsoft.Practices.EnterpriseLibrary这个东西,根据名字猜测和微软有关系(可以翻译为:微软实践企业库). 看到了引入了两个命名空间: using Mic ...

  7. oracle杨树,Microsoft.Practices.EnterpriseLibrary连接Oracle

    Microsoft Enterprise Library用起来也是挺不错的,对于习惯自己写sql语句的开发者们,这个应该挺好的.下面以连接Oracle为例简单介绍下如何连接数据库. 首先下载Micro ...

  8. 微软企业库(Microsoft Enterprise Library Data Access Block)

    1. Dynamic-link library Microsoft.Practices.ObjectBuilder.dll Microsoft.Practices.EnterpriseLibrary. ...

  9. [C#] . [Unity] 使用Microsoft.Practices.Unity 依赖注入

    https://www.cnblogs.com/slardar1978/p/4205394.html ----------------------------------- Unity是微软Patte ...

最新文章

  1. oracle之 手动创建 emp 表 与 dept 表
  2. Wallop下蛋送邀请。
  3. C++构造函数初始化列表
  4. C# 往excel出力数据
  5. float32精度_混合精度对模型训练和推理的影响
  6. 【转】VSTS中版本控制系统Git与TFVC的区别
  7. 关于BSTR数据类型
  8. IEEE Fellow 2020名单揭晓!BDTC 2019重磅嘉宾周伯文、叶杰平、陈宝权上榜
  9. 使用adblock plus浏览器插件屏蔽广告
  10. 计算机设计大赛作品——冬奥可视化
  11. 读《大秦帝国》第二部
  12. 单片机常用芯片系列(二)——DS18B20详解
  13. 在解锁Redmi 5A
  14. 计算机主机可以有几个硬盘,一台电脑可以安装盘几个硬盘?
  15. PHP 图片合成、仿微信群头像
  16. android 状态栏显示 耳机图标显示,Android4.0-4.4 加入支持状态栏显示耳机图标方法(支持带不带MIC的两种耳机自己主动识别)...
  17. vRealize Operations Manager 仪表板示图
  18. 仿权重8高收录面包网pc+手机苹果cmsv8影视网站含迅雷下载N430模板
  19. 一键将手机投屏到电脑上玩,这下摸鱼更方便了
  20. (2020版) 墙裂推荐这十款精选 IntelliJ Idea 插件

热门文章

  1. 基于贝叶斯分类器进行sklearn乳腺癌数据集的分类
  2. Html如何引用公用的JS和CSS
  3. 设计师必备取色技巧!教你在PS里通过照片创建色板
  4. PHP 多层循环 如何跳出循环?
  5. Vue 导出Excel表格,并实现合并单元格方法
  6. go读取/下载文件(大文件分片处理)
  7. eja变送器故障代码al01_eja变送器表头常见错误代码代表含义你造吗?
  8. p5.js创意编程——Q版人像绘制
  9. 美信监控易助力大中型企业实现机房动环一体化监控
  10. 解决IDEA中程序包org.springframework.stereotype不存在问题