使用Microsoft.Practices.EnterpriseLibrary.Data调用存数过程Output参数注意事项
最近拿了一个开源的源码看了下,在调试的过程中发现调用存数过程的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
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参数注意事项相关推荐
- webservice mysql配置文件_在WebService中使用Microsoft.Practices.EnterpriseLibrary.Data配置数据库...
标签: 1. 新建WebApplication1项目 1.1 新建-Web-ASP.NET Empty Web Application--WebApplication1 1.2 添加一个WebForm ...
- Microsoft.Practices.EnterpriseLibrary.Data 数据库操作
出处:Microsoft.Practices.EnterpriseLibMicrosoft.Practices.EnterpriseLibrary.Data 数据库操作 - 米高佐敦 - 博客园Mic ...
- 解决: 未能加载文件或程序集“Microsoft.Practices.EnterpriseLibrary.Data,
在文件夹中找到这几个文件,替换成合适的版本即可
- WCF-004:WCF中也可以使用Microsoft.Practices.EnterpriseLibrary
在WCF服务中我一直是使用ADO.NET 实体模型来连接数据和操作数据库的,刚开始用时觉得比较新鲜,因为linq与sql语句的基本操作都有对应的使用方法,而且能直接获取到数据表的模型,大多情况下还是比 ...
- Microsoft.Practices.EnterpriseLibrary连接Oracle
Microsoft Enterprise Library用起来也是挺不错的,对于习惯自己写sql语句的开发者们,这个应该挺好的.下面以连接Oracle为例简单介绍下如何连接数据库. 首先下载Micro ...
- Microsoft.Practices.EnterpriseLibrary
项目中使用了Microsoft.Practices.EnterpriseLibrary这个东西,根据名字猜测和微软有关系(可以翻译为:微软实践企业库). 看到了引入了两个命名空间: using Mic ...
- oracle杨树,Microsoft.Practices.EnterpriseLibrary连接Oracle
Microsoft Enterprise Library用起来也是挺不错的,对于习惯自己写sql语句的开发者们,这个应该挺好的.下面以连接Oracle为例简单介绍下如何连接数据库. 首先下载Micro ...
- 微软企业库(Microsoft Enterprise Library Data Access Block)
1. Dynamic-link library Microsoft.Practices.ObjectBuilder.dll Microsoft.Practices.EnterpriseLibrary. ...
- [C#] . [Unity] 使用Microsoft.Practices.Unity 依赖注入
https://www.cnblogs.com/slardar1978/p/4205394.html ----------------------------------- Unity是微软Patte ...
最新文章
- oracle之 手动创建 emp 表 与 dept 表
- Wallop下蛋送邀请。
- C++构造函数初始化列表
- C# 往excel出力数据
- float32精度_混合精度对模型训练和推理的影响
- 【转】VSTS中版本控制系统Git与TFVC的区别
- 关于BSTR数据类型
- IEEE Fellow 2020名单揭晓!BDTC 2019重磅嘉宾周伯文、叶杰平、陈宝权上榜
- 使用adblock plus浏览器插件屏蔽广告
- 计算机设计大赛作品——冬奥可视化
- 读《大秦帝国》第二部
- 单片机常用芯片系列(二)——DS18B20详解
- 在解锁Redmi 5A
- 计算机主机可以有几个硬盘,一台电脑可以安装盘几个硬盘?
- PHP 图片合成、仿微信群头像
- android 状态栏显示 耳机图标显示,Android4.0-4.4 加入支持状态栏显示耳机图标方法(支持带不带MIC的两种耳机自己主动识别)...
- vRealize Operations Manager 仪表板示图
- 仿权重8高收录面包网pc+手机苹果cmsv8影视网站含迅雷下载N430模板
- 一键将手机投屏到电脑上玩,这下摸鱼更方便了
- (2020版) 墙裂推荐这十款精选 IntelliJ Idea 插件