C# 获取存储过程 返回的参数Output

public  IList<Gname> GetReadFlei( int fid)
        {
            SqlParameter[] par = {  new SqlParameter( " @fid ",fid),
                                   new SqlParameter( " @RecordCount ",SqlDbType.NVarChar, 30)
                                 };
            par[ 1].Direction = ParameterDirection.Output;

            SqlDataReader DR = SQLHelp.ExecuteReader( " StroGetTest ", CommandType.StoredProcedure, par);
            IList<Gname> List =  new List<Gname>();
           
             while (DR.Read())
            {
                Gname sw =  new Gname();
                sw.id = DR.GetInt32( 0);
                sw.gname = DR.GetString( 1);
                sw.gdir = DR.GetString( 2);
                sw.gfla = DR.GetString( 3);
                List.Add(sw);
            }
            
            DR.Close();
             this.Label1.Text = par[ 1].Value.ToString();
            
             return List;
        }

 

public  static SqlDataReader ExecuteReader( string cmdText, CommandType cmdType,  params SqlParameter[] cmdParms) 

        {
            SqlCommand cmd = new SqlCommand();
            SqlConnection conn = new SqlConnection(Constr);

            try
            {
                ProCommand(cmd, conn, cmdText, cmdType,cmdParms);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                //cmd.Parameters.Clear();//放到这里,返回参数会被清空。
                return rdr;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }

注意:cmd.Parameters.Clear()不能用,否用返回参数会被清空

CREATE  PROCEDURE  StroGetTest
@fid  int ,
@RecordCount  NVarChar( 30) Output
AS

SELECT  @RecordCount = count( 1FROM gname  WHERE fid = @fid;
SELECT id,gname,gdir,gfla  FROM Gname  WHERE fid = @fid;
GO

 

 

你可能感兴趣的:(存储过程)