本文整理汇总了C#中System.Data.SqlClient.SqlCommand.GetCacheKey方法的典型用法代码示例。如果您正苦于以下问题:C# SqlCommand.GetCacheKey方法的具体用法?C# SqlCommand.GetCacheKey怎么用?C# SqlCommand.GetCacheKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlCommand
的用法示例。
在下文中一共展示了SqlCommand.GetCacheKey方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetCacheKey_GivenSqlCommandWithoutParameters_ReturnsKey
public void GetCacheKey_GivenSqlCommandWithoutParameters_ReturnsKey()
{
// arrange
var command = new SqlCommand();
command.CommandText = "SELECT * FROM table WHERE Column1 = 'AStringValue'";
// act
var key = command.GetCacheKey();
// assert
Assert.That(key, Is.EqualTo(command.CommandText));
}
示例2: GetCacheKey_GivenSqlCommandWithParameters_ReturnsKey
public void GetCacheKey_GivenSqlCommandWithParameters_ReturnsKey()
{
// arrange
var command = new SqlCommand();
command.CommandText = "SELECT * FROM table WHERE Column1 = @Parameter1 AND Column2 = @Parameter2";
command.Parameters.Add(new SqlParameter("@Parameter1", "AStringValue"));
command.Parameters.Add(new SqlParameter("@Parameter2", 12));
var expected = "SELECT * FROM table WHERE Column1 = @Parameter1 AND Column2 = @Parameter2 -- {@Parameter1: AStringValue} {@Parameter2: 12}";
// act
var key = command.GetCacheKey();
// assert
Assert.That(key, Is.EqualTo(expected));
}
示例3: GetDataReader
public DbDataReader GetDataReader(SqlCommand command, Func<DbDataReader> query)
{
if (command == null)
{
throw new ArgumentNullException(nameof(command));
}
if (query == null)
{
throw new ArgumentNullException(nameof(query));
}
var key = command.GetCacheKey();
if (storage.ContainsKey(key))
{
return (DbDataReader)storage.Get(key);
}
CacheableDataReader cacheableReader = null;
try
{
var results = query();
cacheableReader = new CacheableDataReader(results);
storage.Add(key, cacheableReader);
return cacheableReader;
}
catch
{
if (cacheableReader != null)
{
cacheableReader.Dispose();
}
throw;
}
}