SQL 存储过程中的 output 怎么用?并且在程序中我需要返回这个output的值怎么返回。
求各位高手指点。急需解答!

解决方案 »

  1.   

    command.parameters["@RoleID"].Direction = ParameterDirection.Output;int rowsAffected = command.ExecuteNonQuery();
    int newID = command.parameters["@RoleID"].value;
      

  2.   

    参见
    http://bbs.5d.cn/Detail.aspx?forum=10&id=1357160&page=1
      

  3.   

    楼主给的分太少啦~~很多人不会来的。 分几种方式,如果是指需要接收一个数据的话
    楼上的方法可以的cmd.Parameters["@PageCount"].Direction = ParameterDirection.Output;如果需要多个返回参数,需要在方法上添加参数out int PageCount, out int ListCount
    我的示例
            public static DataSet ExecuteDataSetByStoredProcedure(string SPName, out int PageCount, out int ListCount, params SqlParameter[] cmdParms)
            {
                using (SqlConnection conn = new SqlConnection(ConnStr))
                {
                    if (conn.State == ConnectionState.Closed) { conn.Open(); }//判断连接是否打开
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = SPName;
                        cmd.CommandType = CommandType.StoredProcedure;
                        PrepareCommand(cmd, cmdParms);
                        cmd.Parameters.Add("@PageCount", SqlDbType.Int);
                        cmd.Parameters["@PageCount"].Direction = ParameterDirection.Output;
                        cmd.Parameters.Add("@ListCount", SqlDbType.Int);
                        cmd.Parameters["@ListCount"].Direction = ParameterDirection.Output;
                        DataSet ds = new DataSet();
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        da.Fill(ds);
                        PageCount = (int)cmd.Parameters["@PageCount"].Value;
                        ListCount = (int)cmd.Parameters["@ListCount"].Value;
                        return ds;
                    }
                }
            }
    然后调用的时候加上相应参数,并在参数前加 out
      

  4.   

    +1论坛签名======================================================================benben_tong:你好!
    截至 2011-10-21 10:10:15 前:
    你已发帖 4 个, 未结贴 1 个;
    结贴率为: 75.00%

    当您的问题得到解答后请及时结贴.

    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
    http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
    http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html如何给分和结贴?
    http://community.csdn.net/Help/HelpCenter.htm#结帖如何给自己的回帖中也加上签名?
    http://blog.csdn.net/q107770540/archive/2011/03/15/6250007.aspx
      

  5.   

    注意用datareader读取的时候,先关闭。
      

  6.   

    谢谢各位的解答,次问题已得到解决,只怪自己粗心遗漏了一个地方:SQL 存储过程:
    ALTER PROCEDURE [dbo].[UP_Red_Red_Mac_select]
    @mac nvarchar(50), --传的参数
    @gametype int ,
    @ip nvarchar(50),   --获取本机的IP
    @code varchar(50) output 
    AS
    declare @codetemp varchar(50)
    declare @idtemp int 
    if(select  count(1) from Red_Mac where  mac=@mac and GameType=@gametype)=0
    begin
         select @idtemp=id, @codetemp=code from red_code where IsAction=1 
         if (@codetemp!='')
         begin
       INSERT INTO [Red_Mac]
               ([mac]
               ,[ip]
               ,[cretime]
               ,[code]
               ,[GameType])
         VALUES
               (@mac,@ip,getdate(),@codetemp,@gametype)
               update red_code set IsAction=0,updatetime=getdate() where id=@idtemp
               select @code=code from Red_Mac where  mac=@mac and GameType=@gametype
                          return 1 --发送成功
               end
               else
                 begin
                   return  -1 --激活码发完
                 end
            end
         else
             begin
                 select @code=code from Red_Mac where  mac=@mac  and GameType=@gametype
               
                 return -2 --已经申请过激活码
             end
    程序的调用:
      public string Get_Code(string mac, int gametype)
            {
                int rowsAffected = 0;
                string code=string.Empty;
                StringBuilder strSql = new StringBuilder();
                SqlParameter[] parameters = {
    new SqlParameter("@mac", SqlDbType.NVarChar,50),
    new SqlParameter("@gametype",  SqlDbType.Int,4),
    new SqlParameter("@ip", SqlDbType.NVarChar,50), 
                        new SqlParameter("@code",SqlDbType.NVarChar,50)
                                            };
                parameters[0].Value = mac;
                parameters[1].Value = gametype;
                parameters[2].Value = WebConfig.GetIp;
                parameters[3].Direction = ParameterDirection.Output;          
              
                int obj = DbHelperSQL.RunProcedure("UP_Red_Red_Mac_select", parameters, out rowsAffected);
                if (obj == 1 ||obj==-2)
                {
                    return  obj+"|"+parameters[3].Value.ToString();
                }
                else
                {
                    return "-1";
                }
            }