C#调用存储过程,并且获得返回值和OutPut字符串
存储过程的代码如下:
ALTER proc [dbo].[Get_Model_ByGroup] ( @groups varchar(200), @msg varchar(50)='' output ) as declare @sqlPage nvarchar(1000) --主语句 begin set @sqlPage='select * from T_Model where moduleid in(select distinct moduleid from T_Groups_Model_Popedom where GroupsID in('+@groups+'))' end exec(@sqlPage) if @@error<>0 begin set @msg='查询操作失败!' return 0 end else begin set @msg='查询操作成功!' return 1 end
调用存储过程的C#代码如下:
try { SqlConnection conn = new SqlConnection(connectString); SqlCommand comm = new SqlCommand("Get_Model_ByGroup", conn); comm.CommandType = CommandType.StoredProcedure; SqlParameter paramgroups = new SqlParameter("@groups", SqlDbType.VarChar, 200); paramgroups.Value = groups; comm.Parameters.Add(paramgroups); SqlParameter paramreturnmsg = new SqlParameter("@msg", SqlDbType.VarChar, 50); paramreturnmsg.Direction = ParameterDirection.Output; comm.Parameters.Add(paramreturnmsg); SqlParameter paramreturnvalue = new SqlParameter("@return", SqlDbType.Int, 4); paramreturnvalue.Direction = ParameterDirection.ReturnValue; comm.Parameters.Add(paramreturnvalue); SqlDataAdapter da = new SqlDataAdapter(comm); da.Fill(result.returnDt); result.returnMsg = paramreturnmsg.Value.ToString(); result.returnValue = (int)paramreturnvalue.Value; } catch (Exception) { throw; } return result;
SqlParameter.Direction 属性
获取或设置一个值,该值指示参数是只可输入、只可输出、双向还是存储过程返回值参数。
ParamenterDirection.Inupt为默认值,只可输入
ParameterDirection.ReturnValue,只可输出