日期:2014-05-18  浏览次数:20769 次

SqlDataReader 无法得到存储过程的记录总数
C# code

public static SqlDataReader ExeDr(CommandType Tyle, string SqlStr, params SqlParameter[] parameters)
        {
            SqlCommand ComDr = GetCommand(Tyle, SqlStr, parameters);
            SqlDataReader Dr = ComDr.ExecuteReader(CommandBehavior.CloseConnection);
            ComDr.Parameters.Clear();
            return Dr;
        }        


SqlParameter[] parms = new SqlParameter[] 
            { 
                new SqlParameter("@tbName", SqlDbType.VarChar, 255),
                new SqlParameter("@tbFields", SqlDbType.VarChar, 1000),
                new SqlParameter("@OrderField", SqlDbType.VarChar, 255),
                new SqlParameter("@PrimaryField", SqlDbType.VarChar, 255),
                new SqlParameter("@PageSize", SqlDbType.Int),
                new SqlParameter("@PageIndex", SqlDbType.Int),
                new SqlParameter("@OrderType", SqlDbType.Bit),
                new SqlParameter("@strWhere", SqlDbType.VarChar, 1500),
                new SqlParameter("@Total", SqlDbType.Int),

            };
            parms[0].Value = "Company";
            parms[1].Value = "Id,CompanyName";
            parms[2].Value = "AddTime";
            parms[3].Value = "Id";
            parms[4].Value = PageSize;
            parms[5].Value = PageIndex;
            parms[6].Value = 0;
            parms[7].Value = "State=1";
            parms[8].Direction = ParameterDirection.Output;

            Total = (int)parms[8].Value;//////////////////******这里出错******///////////////

            SqlDataReader Dr = Rsyll.DAL.SqlHelper.ExeDr(CommandType.StoredProcedure, "SqlDataPaging", parms);
            while (Dr.Read())
            {                
                Company_Model One = ReturnCompanyDemo(Dr);
                List.Add(One);
                //Total = (int)Dr[0];                
            }            
            Dr.Close();
            Dr.Dispose();            
            return List;



------解决方案--------------------
你已经用传出参数了:
new SqlParameter("@Total", SqlDbType.Int),
parms[8].Direction = ParameterDirection.Output;

就可以这么获取
//如果存储过程不返回记录信息,就不要用ExecuteReader,用ExecuteNonQuery!
sqlComm.ExecuteNonQuery();
strng c= sqlComm.Parameters["@pageCountint "].Value.ToString()