Monday, 8 December 2008

Encrypting config sections on the fly in .Net

Question:
In a asp.net application I want to keep some config sections encrypted. How do I do this?

Put config sections into a separate file (.Net)

Question:
How do I put DotNet config sections into separate file?

Thursday, 20 November 2008

Useful queries for optimizing Sql Server 2005

Question:
How can I figure out the queries that take most resources on a server.

Answer:
We use this query.
It lists the worst queries (I/Os) on your database since the last restart.

select top 5 (total_logical_reads/execution_count) as avg_logical_reads,
(total_logical_writes/execution_count) as avg_logical_writes,
(total_physical_reads/execution_count) as avg_physical_reads,
Execution_count, statement_start_offset, p.query_plan, q.text
from sys.dm_exec_query_stats
cross apply sys.dm_exec_query_plan(plan_handle) p
cross apply sys.dm_exec_sql_text(plan_handle) as q
order by (total_logical_reads + total_logical_writes)/execution_count Desc

I grabbed it somewhere form TechNet: Monitoring SQL Server health
Wednesday, 15 October 2008

How to use the HttpContext cache in a non-web (test) environment

Question:
How to test a class that relies on HttpContext.Current.Cache (or so) in order to work.
If you test this in NUnit, for example, there will be no current HttpContext around.
So abstract the access to the Context away and in the property you'll provide another cache if need is.

Attach Debugger macro in VS 2005/ 2008

Question:
How to use a macro in Visual Studio 2005/2008 to attach a debugger to a process?

Tuesday, 14 October 2008

Call private methods of an object from a test assembly. (NUnit)

I have to test a class of Foo from my test assembly Foo.Test.
Unfortunately, many methods I should test are not public.

This class uses reflection to call private methods on any type or object for testing.

using System;
using System.Reflection;

namespace TestUtilities
{
public class TestHelper
{
public static object InvokeStaticMethod(System.Type type, string methodName, object[] parameters)
{
BindingFlags bindFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
return InvokeMethod(type, methodName, null, parameters, bindFlags);
}
public static object InvokeInstanceMethod(System.Type type, object instance, string methodName, object[] parameters)
{
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
return InvokeMethod(type, methodName, instance, parameters, bindFlags);
}
private static object InvokeMethod(System.Type type, string methodName, object instance, object[] parameters, BindingFlags bindFlags)
{
MethodInfo methInfo;
methInfo = type.GetMethod(methodName, bindFlags);
if (methInfo == null)
{
throw new ArgumentException(String.Format("There is no method with name {0} on type {1}", methodName, type.Name));
}

object result = methInfo.Invoke(instance, parameters);
return result;
}

}
}

Tuesday, 1 July 2008

Get the date portion of a datetime for sql server 2000

Using T-SQL in SQL Server 2000 you sometimes want compare a given datetime value with date by ignoring the time portion of the datetime value.

Here are three ways to strip off the time which gives the following:
2007-06-21 11:23:41.321 => 2007-06-21 00:00:00.000

1) convert to string in format (ISO yymmdd), then cast to datetime
- select on 500000 items takes 1703 ms

SELECT CAST( CONVERT( CHAR(8), GetDate(), 112) AS DATETIME)

2) convert to number of days in float, cut all that is smaller than a day, then cast to datetime
- select on 500000 items takes 228 ms

SELECT CAST( FLOOR( CAST( GETDATE() AS FLOAT ) ) AS DATETIME )

3) get the difference form zero date as number of days, then add this number to zero date.
- select on 500000 items takes 184 ms

SELECT DATEADD(d, 0, DATEDIFF(d, 0, GETDATE()))



Performance tested like this.

SET DATEFORMAT dmy
SET STATISTICS IO ON
SET STATISTICS TIME ON
select myDateTime
from #dummy
where CAST( FLOOR( CAST( myDateTime AS FLOAT ) ) AS DATETIME ) = '02/07/2007'
SET STATISTICS TIME OFF
SET STATISTICS IO OFF