本文介绍了过程返回值但代码返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 当我通过代码调用该过程时,它返回null 当我在SSMS中运行该过程时,它返回值 .DBML如下 < 函数 名称 = dbo.AAA_GET_CODE 方法 = AAA_GET_CODE > < 参数 名称 = p_SYSTEM_ID 参数 = p_SYSTEM_ID 类型 = System.Int32 DbType = Int / > < ElementType 名称 = AAA_get_CODEResult > < 列 名称 = CODEDecode 类型 = System.String DbType = VarChar(15) CanBeNull = false / > < / ElementType > < / Function > 其designer.cs如下 [ global :: System.Data.Linq.Mapping.FunctionAttribute(Name = dbo.AAA_GET_CODE)] public ISingleResult< AAA_get_CODEResult> AAA_GET_CODE([ global :: System.Data.Linq.Mapping.ParameterAttribute(DbType = Int)] System.Nullable< int> p_SYSTEM_ID) { IExecuteResult result = this .ExecuteMethodCall( this ,((MethodInfo)(MethodInfo.GetCurrentMethod())),p_SYSTEM_ID); return ((ISingleResult< AAA_get_CODEResult>)(result.ReturnValue)); } AAA_get_CODEResult如下 public partial class AAA_get_CODEResult { private string _CODEDecode; public AAA_get_CODEResult() {} [ global :: System.Data.Linq.Mapping.ColumnAttribute(Storage = _ CODEDecode ,DbType = VarChar(15),CanBeNull = false )] public string CODEDecode { get { return 此 ._ CODEDecode; } set { if (( this ._ CODEDecode!= value )) { this ._ CODEDecode = value ; } } } } 调用代码如下 尝试 { return appl.AAA_GET_CODE(SYSTEMId).FirstOrDefault()。CODEDecode; } catch (SqlException ex) { Throw; } 存储过程如下 CREATE PROCEDURE [dbo]。[AAA_GET_CODE] - 在此处添加存储过程的参数 @ p_SYSTEM_ID INT AS DECLARE @ CODE_DECODE VARCHAR ( 15 ) BEGIN BEGIN TRY SELECT @CODE_DECODE = CODE_DECODE FROM [dbo]。[eCODES] 其中 SYSTEM_ID = @ p_SYSTEM_ID END TRY BEGIN CATCH - 声明 DECLARE @ ErrorMessage NVARCHAR ( 4000 ), @ ErrorProc NVARCHAR ( 126 ), @ ErrorLineNo INT ; - 确保ACID测试通过。将数据库恢复为已知状态 IF (XACT_STATE())!= 0 BEGIN ROLLBACK TRANSACTION ; END - 错误处理 - 抓取错误代码并抛出 SELECT @ ErrorLineNo = ERROR_LINE(), @ ErrorMessage = ERROR_MESSAGE(), @ ErrorProc = ERROR_PROCEDURE(); RAISERROR (' 发生错误%s %s。行%d', 16 , 1 , @ ErrorMessage , @ ErrorProc , @ ErrorLineNo ) WITH SETERROR; RETURN 1 END CATCH SELECT @ CODE_DECODE AS CODE_DECODE END 当我用@p_SYSTEM_ID = 907173916调用此程序时 返回CODE_DECODE ='aaaaaa' 当我运行代码来调用该程序时,代码运行完全正常并且已经调试,但它返回null。 请帮助解决方案 在DECLARE @CODE_DECODE VARCHAR(15)之前添加Set nocount on,看看会发生什么。谢谢, Hi,When I call the procedure through code , it returns nullWhen I run the procedure in SSMS it returns value.DBML is as follows<Function Name="dbo.AAA_GET_CODE" Method="AAA_GET_CODE"> <Parameter Name="p_SYSTEM_ID" Parameter="p_SYSTEM_ID" Type="System.Int32" DbType="Int" /> <ElementType Name="AAA_get_CODEResult"> <Column Name="CODEDecode" Type="System.String" DbType="VarChar(15)" CanBeNull="false" /> </ElementType> </Function>Its designer.cs is as follows[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.AAA_GET_CODE")] public ISingleResult<AAA_get_CODEResult> AAA_GET_CODE([global::System.Data.Linq.Mapping.ParameterAttribute(DbType = "Int")] System.Nullable<int> p_SYSTEM_ID){ IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), p_SYSTEM_ID);return ((ISingleResult<AAA_get_CODEResult>)(result.ReturnValue));}AAA_get_CODEResult is as follows public partial class AAA_get_CODEResult{private string _CODEDecode;public AAA_get_CODEResult(){}[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CODEDecode", DbType="VarChar(15)", CanBeNull=false)]public string CODEDecode{get{return this._CODEDecode;}set{if ((this._CODEDecode != value)){this._CODEDecode = value;}}}}Calling code is as followstry { return appl.AAA_GET_CODE(SYSTEMId).FirstOrDefault().CODEDecode; } catch (SqlException ex) {Throw;}The stored procedure is as followsCREATE PROCEDURE [dbo].[AAA_GET_CODE] -- Add the parameters for the stored procedure here @p_SYSTEM_ID INT ASDECLARE @CODE_DECODE VARCHAR(15)BEGIN BEGIN TRY SELECT @CODE_DECODE = CODE_DECODE FROM [dbo].[eCODES] where SYSTEM_ID = @p_SYSTEM_ID END TRY BEGIN CATCH -- Declares DECLARE @ErrorMessage NVARCHAR(4000), @ErrorProc NVARCHAR(126), @ErrorLineNo INT; -- Ensure ACID Test Pass. Return Database back to known state IF (XACT_STATE()) != 0 BEGIN ROLLBACK TRANSACTION; END -- ERROR HANDLING - Grab Error code and throw SELECT @ErrorLineNo = ERROR_LINE(), @ErrorMessage = ERROR_MESSAGE(), @ErrorProc = ERROR_PROCEDURE(); RAISERROR('Error %s occurred in %s. Line %d', 16, 1, @ErrorMessage, @ErrorProc, @ErrorLineNo) WITH SETERROR; RETURN 1 END CATCH SELECT @CODE_DECODE AS CODE_DECODE END When I call this procedure with @p_SYSTEM_ID = 907173916It returns CODE_DECODE = ‘aaaaaaa’When I run the code to call the procedure, code runs perfectly fine and debuged , but it is returning null.Please help 解决方案 Add "Set nocount on" before the DECLARE @CODE_DECODE VARCHAR(15) to see what happens. Thanks, 这篇关于过程返回值但代码返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-15 23:34