2

我正在阅读Microsoft.ApplicationBlocks.Data的代码(代码在这里

但我注意到一些奇怪的事情:

在其中一个函数(executeNonQuery)中,他尝试从缓存中读取 Sp 的参数,如果不存在,他将它们放入缓存(不是 asp.net 缓存 - 而是一个内部 HashSet)

public static int ExecuteNonQuery(string connectionString, string spName, params SqlParameter[] parameterValues)
    {
        if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
        if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
        // If we receive parameter values, we need to figure out where they go
        if ((parameterValues != null) && (parameterValues.Length > 0))
        {
            // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
            //------------------------------------

            SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

           //------------------------------------
            // Assign the provided values to these parameters based on parameter order
            AssignParameterValues(commandParameters, parameterValues);
            // Call the overload that takes an array of SqlParameters
            return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
        }
        else
        {
            // Otherwise we can just call the SP without params
            return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
        }
    }

请看隔离线。

他们为什么这样做?如果我在 Cache 中有参数,它对我有什么帮助?无论如何,我都会从我的 dal 发送参数……(而且我必须将我的参数发送给 SP)……

我错过了什么?

4

1 回答 1

0

如果没有缓存,他们最终会SqlCommandBuilder.DeriveParameters为每个调用调用 ExecuteNonQueryMSDN指出:

DeriveParameters 需要额外调用数据库来获取信息。如果事先知道参数信息,则通过显式设置信息来填充参数集合会更有效。

因此,缓存检索到的参数信息以避免这些额外调用是有意义的。

更新:

无论如何,我都会从我的 dal 发送参数……(而且我必须将我的参数发送给 SP)……

请注意int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)重载——您可能会也可能不会发送参数。

于 2012-10-25T17:16:06.773 回答