Enterprise library caching parameters on stored procs?

谁说我不能喝 提交于 2019-12-10 10:04:20

问题


I'm trying to standardise some data access code with my colleagues. One of the aforementioned colleagues asserts that the EntLib Data Access Block trys to cache parameters on stored proc calls.

I've had a look in reflector and there is some evidence that it could be caching them. But I don't think it does in the following situation.

    public Dictionary<long, string> GetQueue(int maxItems)
    {
        var sq = new SqlDatabase(_connString.ConnectionString);

        var result = new Dictionary<long, string>();

        using (var cmd = (SqlCommand)sq.GetStoredProcCommand("dbo.GetQueue"))
        {
            sq.AddInParameter(cmd, "maxItems", DbType.Int32, maxItems);

            var reader =  cmd.ExecuteReader(CommandBehavior.CloseConnection);

            while (reader.Read())
            {
                long id = reader.GetInt64(reader.GetOrdinal("id"));
                string fileName = reader.GetString(reader.GetOrdinal("meta_data_filename"));

                result.Add(id, fileName);
            }
        }

        return result;
    }

Can anyone confirm or deny this?

I'm using EntLib 4.1


回答1:


It definetly used to, I ripped the code out and threw in in my library.

it used sp_help and parsed the output to determine the data types.

These days, I ripped the code out, .Net is much much better about adding parameters.

cmd.Parameters.AddWithValue("@name",somevalue)

in your example of you keep reflectoring ... you will find it being done down this path GetStoredProcCommand()

You will get a Command object back, already populated with parameters

The ent lib code is copyrighted, but the code is almost identical to this

http://code.google.com/p/dbdotnet/source/browse/trunk/ParameterCache.cs




回答2:


As far as I can tell it doesn't cache the parameters. Using the same instance of a Database object I called DiscoverParameters multiple times while running a trace. Each time I call DiscoverParameters I can see a [sys].[sp_procedure_params_100_managed] so it looks like it's making the round trip every time.

Here's an example of how to do it yourself that's seems like it might be alright:

http://davidhayden.com/blog/dave/archive/2006/11/03/CachingStoredProcedureParameters.aspx



来源:https://stackoverflow.com/questions/704794/enterprise-library-caching-parameters-on-stored-procs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!