C# net和Sp';缓存中的参数?

C# net和Sp';缓存中的参数?,c#,.net,ado.net,sqlparameter,C#,.net,Ado.net,Sqlparameter,我正在读() 但我注意到了一些奇怪的事情: 在其中一个函数(ExecuteOnQuery)中,他尝试从缓存中读取Sp的参数,如果不存在,则将其放入缓存(不是asp.net缓存,而是内部哈希集) 请看隔离线 他们为什么这样做?如果我在缓存中有参数,这对我有什么帮助?无论如何,我将从我的dal发送参数(我必须将我的参数发送到SP) 我缺少什么?如果没有缓存,他们最终会为每次调用ExecuteOnQuery调用SqlCommandBuilder.DeriveParameters,其状态为: Deriv

我正在读()

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

在其中一个函数(ExecuteOnQuery)中,他尝试从缓存中读取Sp的参数,如果不存在,则将其放入缓存(不是asp.net缓存,而是内部哈希集)

请看隔离线

他们为什么这样做?如果我在缓存中有参数,这对我有什么帮助?无论如何,我将从我的dal发送参数(我必须将我的参数发送到SP)


我缺少什么?

如果没有缓存,他们最终会为每次调用
ExecuteOnQuery
调用
SqlCommandBuilder.DeriveParameters
,其状态为:

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

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

更新:

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


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

如果参数值与CommandParameters不匹配,AssignParameterValues()可能引发异常,在“触碰”数据库之前停止执行?你能发布那个方法吗?@StingyJack我检查了源代码,如果两个参数数组不匹配,它会引发异常。我为什么还需要参数信息?为什么我需要呼叫数据库以获取SP信息?发送参数+值有什么问题?请记住,
SQLPARAMETER
有一个
ctor
公共SQLPARAMETER(字符串参数名,对象值)
不需要参数信息,只需要名称和值。…@RoyiNamir您可以使用
SqlParameter
ctor(或
AddWithValue(string,object)
);但是,您需要假设从CLR类型到T-SQL类型的隐式转换是准确的。在某些情况下,情况可能并非如此;e、 g.存储的proc参数是
varchar(2)
,您发送字符串“Hello world”,该字符串将隐式转换为
nvarchar(11)
。除了大小问题外,您可能还有性能问题。好的,那么如果sql DB表的参数类型为varchar(2)(并且我将发送一个字符串),那么在将其发送到SP之前,我应该检查这个
DeriveParameters
东西吗?这就是为什么存在此
DeriveParameters
的原因吗?逐个验证?例如,假设您有一个存储过程
getCustomerByState(@state char(2))
。作为.Net开发人员,如果您事先知道SP参数,则无需调用DeriveParameters(您只需创建一个
新的SqlParameter(“@state”,SqlDbType.VarChar,2)
)?但是请注意,所讨论的API允许您简单地将字符串“CA”作为参数传递(不指定参数的名称或类型)。感谢您的回答,如果sqlParmeters是从
DeriveParameters
填充的,并且我将长字符串发送到varchar(2)参数,它会异常还是截断?
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);
        }
    }