Using Events in .NET

Consuming events in .NET is possible on couple of ways. So, if object type Car contains an event SaySpeed it can be consumed on the following ways:

  • Usual way
    car.SaySpeed += car_SaySpeed;
    

    where car_SaySpeed is function which implements SaySpeed delegate like:

    void car_SaySpeed(object sender, CarEventArgs e)
    {
        ...
    }
    
  • Another method is by using anonymous methods as
    car.SaySpeed += delegate
    			{
    				...
    			};
    
  • And a clean one, by using lambda expressions
    car.SaySpeed += (sender, e) => { 
    				... 
    			};
    

WordPress Permalinks Error

Have you tried to run WordPress with the permalinks on IIS without success? After choosing any of the custom permalinks, yu got 404 page not found? Than your problem is in the web.config file. As it is not writable from the web, you should manually update it by adding the following section under system.webServer node:

<rewrite>
		<rules>
			<rule name="wordpress" patternSyntax="Wildcard">
				<match url="*" />
				<conditions>
					<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
					<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
				</conditions>
			<action type="Rewrite" url="index.php" />
			</rule>
		</rules>
</rewrite>

SQL Server: Get cached plans

If you ever wanted to see which SQL plans are cached and how many times called, you can use the following query:

SELECT [cp].[refcounts] ,
[cp].[usecounts] ,
[cp].[objtype] ,
[st].[dbid] ,
[st].[objectid] ,
[st]. ,
[qp].[query_plan], [cp].[plan_handle]
FROM sys.dm_exec_cached_plans cp
	CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
	CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) qp
	INNER JOIN sys.[databases] d ON st.[dbid] = d.[database_id]
WHERE d.[name] LIKE 'YOUR_DATABASE_NAME'
ORDER BY [cp].[usecounts] DESC

Provided plan handle can be used in

DBCC FREEPROCCACHE

as parameter to remove just selected sql plan from the cache.

SQL Server: How to PRINT statement during loop execution

Have you tried to print some information message during some loop execution on SQL Server? So statements like

DECLARE @i INT
DECLARE @msg AS VARCHAR(20)
SET @i = 0
WHILE <condition>
BEGIN
SET @msg = 'Updating ' + CAST(@i AS VARCHAR(30))
PRINT @msg
UPDATE TOP(50) myTable SET col1 = 0 WHERE <update condition>
WAITFOR DELAY '000:00:10'
SET @i = @i + 1
END

print out your messages just when your loop completes (or print in a blocks of messages from once).

But this is not what you wanted. You do not see progress of UPDATE statements. The reason for this is because SQL Server does not allow any interactivity during batch execution. Therefore, PRINT statement buffers it’s output.

To get message whithin the loop execution you should use

REISEERROR(…) WITH NOWAIT 

statement with the severity 10.

So loop will look like:

DECLARE @i INT
DECLARE @msg AS VARCHAR(20)
SET @i = 0
WHILE <condition>
BEGIN
SET @msg = 'Updating ' + CAST(@i AS VARCHAR(30))
RAISERROR(@msg, 10, 0 ) WITH NOWAIT
UPDATE TOP(50) myTable SET col1 = 0 WHERE <update condition>
WAITFOR DELAY '000:00:10'
SET @i = @i + 1
END

On this way you will see messages “Updating 1”, “Updating 2” … during loop execution.