必须在控件离开当前方法ObjectCache之前指定out参数'recordsReturned'

时间:2014-01-26 18:48:46

标签: c# caching

我一直试图找出如何解决上面的错误,如何将值分配给recordsReturned,这是一个来自数据库的输出参数,并将其添加到ObjectCache。

在缓存数据之前我可以获取值,但我无法将其添加到缓存中,我已经尝试过并参考,但没有运气。

希望有比我更多经验的人可以帮助解决我的问题。

我在下面添加了我的代码:

public class RetrieveTravelGuideForSearchResults : IRetrieveTravelGuideForSearchResults
    {
        private readonly string _dbConn;
        public RetrieveTravelGuideForSearchResults()
            {
                _dbConn = ConfigurationManager.ConnectionStrings["ADO_DBConn"].ConnectionString;
            }

        public IEnumerable<DisplayTravelGuideForSearchResults> DisplayTravelGuideSearchResults(string id,out int recordsReturned)
        {
            string cacheData                = "DisplayTravelGuideSearchResults" + RegexHelpers.RegexRemoveAllInvalidCharactersAndSpace(id.ToLower());
            ObjectCache travelGuideCache    = MemoryCache.Default;
            var objectInCache               = travelGuideCache.Get(cacheData) as IEnumerable<DisplayTravelGuideForSearchResults>;

            //recordsReturned = 0;

            if (objectInCache != null)
            {
                recordsReturned = 0; //How can I get value from output parameter and add it here
                return objectInCache;
            }
            const string spName = "dbo.spFTSTravelGuide";
            //recordsReturned = 0;
            using (var cn = new SqlConnection(_dbConn))
            {
                using (var cmd = new SqlCommand(spName, cn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@SearchPhrase", SqlDbType.VarChar, 100));
                    cmd.Parameters["@SearchPhrase"].Value = id;

                    cmd.Parameters.Add(new SqlParameter("@RecordsReturn", SqlDbType.Int));
                    cmd.Parameters["@RecordsReturn"].Direction = ParameterDirection.Output;


                    var data = new List<DisplayTravelGuideForSearchResults>();
                    try
                    {
                        cn.Open();
                        using (var rdr = cmd.ExecuteReader(CommandBehavior.Default))
                        {
                            if (rdr.HasRows)
                            {
                                while (rdr.Read())
                                {
                                    data.Add(new DisplayTravelGuideForSearchResults
                                    {

                                        Country = (string)rdr["TravelGuideCountry"],
                                        Description = RegexHelpers.RegexRemoveHtmlTags((string)rdr["TravelGuideDescription"])
                                    });
                                }
                            }
                        }

                        var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(15) };
                        travelGuideCache.Add(cacheData, data, policy);
                        recordsReturned = cmd.Parameters["@RecordsReturn"].Value as int? ?? 0;

                        return data;
                    }
                    catch (SqlException ex)
                    {
                        throw new ApplicationException(ex.InnerException.ToString());
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException(ex.InnerException.ToString());
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

得到了我的问题的答案,应该已经完成​​

recordsReturned = cmd.Parameters["@RecordsReturn"].Value as int? ?? 0;
travelGuideCache.Add("recordsReturned", recordsReturned, policy);

然后

if (objectInCache != null)
            {
                recordsReturned = travelGuideCache.Get("recordsReturned") as int? ?? 0;
                return objectInCache;
            }

现在获取输出参数值并允许我对其进行缓存。